我最近遇到了将Javascript请求发布到另一个域的问题.默认情况下,不允许将XHR发布到其他域.
按照http://enable-cors.org/的说明,我在其他域上启用了此功能.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Methods" value="GET,PUT,POST,DELETE,OPTIONS" />
<add name="Access-Control-Allow-Headers" value="Content-Type" />
</customHeaders>
</httpProtocol>
</system.webServer>
</configuration>
Run Code Online (Sandbox Code Playgroud)

现在一切正常,但是在发回工作200响应之前仍然会返回405响应.
Request URL:http://testapi.nottherealsite.com/api/Reporting/RunReport
Request Method:OPTIONS
Status Code:405 Method Not Allowed
Request Headersview source
Accept:*/*
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-GB,en-US;q=0.8,en;q=0.6
Access-Control-Request-Headers:origin, content-type, accept
Access-Control-Request-Method:POST
Connection:keep-alive
Host:testapi.nottherealsite.com
Origin:http://test.nottherealsite.com
Referer:http://test.nottherealsite.com/Reporting
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1
Response Headersview source
Access-Control-Allow-Headers:Content-Type
Access-Control-Allow-Methods:GET,PUT,POST,DELETE,OPTIONS
Access-Control-Allow-Origin:*
Allow:POST
Cache-Control:private
Content-Length:1565
Content-Type:text/html; charset=utf-8
Date:Tue, 18 Sep 2012 14:26:06 GMT …Run Code Online (Sandbox Code Playgroud) 我正在调用(Ajax请求)一个WCF REST服务,该请求是一个跨域请求.
如果我在同一个域中部署我的服务,一切都像奶油一样.最终在生产中,该服务将位于不同的域中.
我正在使用jQuery 1.5.2.我的服务给我一个错误说:
errorThrown: "jQuery15208493315000087023_1334089616458 was not called"
textStatus: "parsererror"
Run Code Online (Sandbox Code Playgroud)
虽然在Firefox中我可以看到JSON值,但执行属于Ajax请求的错误处理程序.
我的Ajax请求是:
function CallService() {
$.ajax({
type: "GET",
url: "http://SomeService/EmpService.svc/GetValues?dv=1455",
contentType: "application/json; charset=utf-8",
dataType: "jsonp",
processdata: false,
success: function (data) {
ServiceSucceeded(data);
},
error: function (jqXHR, textStatus, errorThrown) {
debugger;
alert("Service Error");
ServiceFailed(jqXHR, textStatus, errorThrown);
}
});
}
Run Code Online (Sandbox Code Playgroud)
在WCF服务端,我已将CrossDomainScriptAccess配置为true:
<webHttpBinding>
<binding name="webHttpBindingWithJsonP"
crossDomainScriptAccessEnabled="true" />
</webHttpBinding>
Run Code Online (Sandbox Code Playgroud)
我从服务器获得的JSON响应是:
[{"Message": "Stop On Duty", "MessageTime": "\/Date(1334068773893-0500)\/"},
{"Message": "Start On Duty", "MessageTime": "\/Date(1334068763540-0500)\/"},
{"Message": "App_testing_4102012924am", "MessageTime": "\/Date(1334068533627-0500)\/"},
{"Message": "Kunal_testing_4102012924am", "MessageTime": "\/Date(1334067945510-0500)\/"},
{"Message": "Alert: …Run Code Online (Sandbox Code Playgroud)