我正在调用(Ajax 请求)WCF REST 服务,并且该请求是跨域请求。
如果我在同一个域中部署我的服务,一切都会像奶油一样工作。最终在生产中,该服务将位于不同的域中。
我使用的是 jQuery 1.5.2。我的服务返回一条错误消息:
errorThrown: "jQuery15208493315000087023_1334089616458 was not called"
textStatus: "parsererror"
尽管在 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);
}
});
}
在 WCF 服务端,我已将 CrossDomainScriptAccess 配置为 true:
<webHttpBinding>
<binding name="webHttpBindingWithJsonP"
crossDomainScriptAccessEnabled="true" />
</webHttpBinding>
我从服务器得到的 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: Door Open", "MessageTime": "\/Date(1334066280963-0500)\/"}]
我是否缺少设置中的任何内容?如果服务移至同一域,整个代码可以正常工作。
我查看了类似的帖子,但无法完成这项工作。
最佳答案
好吧,我自己想出来了。解决方案是修改保存服务详细信息的配置文件
我在配置文件中添加了标准端点和绑定(bind)
<standardEndpoints>
<webScriptEndpoint>
<standardEndpoint crossDomainScriptAccessEnabled="true">
</standardEndpoint>
</webScriptEndpoint>
</standardEndpoints>
<bindings>
<webHttpBinding>
<binding name="webHttpBindingWithJsonP"
crossDomainScriptAccessEnabled="true" />
</webHttpBinding>
关于jquery - 跨域 jQuery Ajax 请求和 WCF REST 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10096449/