我有一个服务,在chrome中返回正确的数据,但对于firefox和ie9以错误结束.看起来GET返回200 OK代码,但仍然以错误回调结束.我通过backbonejs获取数据(使用
jquery.getJson和ajax我得到相同的结果).如果我尝试从远程服务器或本地获取数据,我也会得到相同的结果.
Chrome:版本23.0.1271.64 m
FF:16.0.2
IE9:9.0.8112.16421
WCF:
[OperationContract]
[WebInvoke(Method = "GET", UriTemplate = "/getData/{name}", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json)]
List<Names> getData(string name);
的serviceUrl:
"http://serverABC:4000/myService.svc/getData/test"
从javascript获取:
通过backbone或jqueryGetJson():
$.getJSON("http://serverABC:4000/myService.svc/getData/test", function () {
alert("success");
})
.success(function () { alert("second success"); })
.error(function (result) {
console.log('error:', result);
})
结果:
“http:// serverABC:4000 / myService.svc / getData / test 200 OK 70ms”
标题:
Response Headers
Cache-Control private
Content-Length 6544
Content-Type application/json; charset=utf-8
Date Fri, 16 Nov 2012 14:09:46 GMT
Server Microsoft-IIS/7.5
Set-Cookie ASP.NET_SessionId=s3aguluzip0dw135glbxlwwf; path=/; HttpOnly
X-AspNet-Version 4.0.30319
X-Powered-By ASP.NET
Request Headers
Accept application/json, text/javascript, */*; q=0.01
Accept-Encoding gzip, deflate
Accept-Language en-US,en;q=0.5
Connection keep-alive
Host svgwbip93:4000
Origin http://localhost:51280
Referer http://localhost:51280/Default.aspx?ReturnUrl=%2f
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:16.0) Gecko/20100101 Firefox/16.0
调试结果:
readyState 0
responseText ""
status 0
**statusText "error"**
abort function()
always function()
complete function()
done function()
error function()
fail function()
getAllResponseHeaders function()
getResponseHeader function()
overrideMimeType function()
pipe function()
progress function()
promise function()
setRequestHeader function()
state function()
statusCode function()
success function()
then function()
toString function()
响应: – 是空的(这很可能是问题所在(但正如我在Chrome中提到的,我得到了正确的json数据)
编辑1:
我试图用fiddler得到原始响应,我得到了JSON.最大的问题是为什么回调会出错.
这是我的原始回复:
HTTP/1.1 200 OK
Cache-Control: private
Content-Length: 29
Content-Type: application/json; charset=utf-8
Server: Microsoft-IIS/7.5
Set-Cookie: ASP.NET_SessionId=kuv3g0r2dgmu5bpaoayj5lic; path=/; HttpOnly
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Fri, 16 Nov 2012 19:32:58 GMT
{"PlatformDrawingsResult":[]}
我验证了json – 看起来没问题,所以问题是什么……嗯.我忘了提到我也在使用requirejs(不确定是否会带来一些亮点……)
干杯,米罗
最佳答案 解决方案是(感谢jwkeenan):
I put this line at the beggining of each method in my web service and
now all browsers work.
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
并让它在ie9中工作我需要将其添加到我的网络应用程序:
$.support.cors = true;