jQuery .ajax()函数总是返回错误
朋友,我迷失在这里。jQuery .ajax()函数总是返回错误
我有这样的WCF REST服务以JSON格式返回数据:http://189.126.109.249/ieptb/Cidades?uf=SP
我可以使用asp.net web表单应用程序访问,并且还,我可以使用Windows Phone应用程序访问它。但是我没有使它在一个简单的jQuery。$ Ajax()调用下工作。我的jQuery总是返回一个错误。如果你看看我的代码,你会看到我有一个捕捉错误的函数。
这里是我使用的脚本:我已经与小提琴手测试http://jsfiddle.net/n6sLQ/4/
,它表明我,HTTP响应是200(OK),即使它让我看到JSON数组返回,就像这样:
HTTP/1.1 200 OK
Via: 1.1 CM-SRV03
Connection: Keep-Alive
Proxy-Connection: Keep-Alive
Content-Length: 2360
Date: Tue, 29 Nov 2011 13:02:44 GMT
Content-Type: application/json; charset=utf-8
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
X-AspNet-Version: 4.0.30319
Cache-Control: private
[{"Nome":"AGUAS DE LINDOIA","Uf":"SP"},{"Nome":"AMERICANA","Uf":"SP"},{"Nome":"AMPARO","Uf":"SP"}]
我不知道什么是错我的jQuery ...
$.ajax({
url: "http://189.126.109.249/ieptb/Cidades?uf=SP",
contentType:"application/json",
dataType: "json",
error: function (x, e) {
if (x.status === 0) {
alert('You are offline!!\n Please Check Your Network. ' + x.reponseText);
}
else if (x.status == 404) {
alert('Requested URL not found.');
} else if (x.status == 500) {
alert('Internel Server Error.');
} else if (e == 'parsererror') {
alert('Error.\nParsing JSON Request failed.');
} else if (e == 'timeout') {
alert('Request Time out.');
} else {
alert('Unknow Error.\n' + x.responseText);
}
},
success: function (cidades) {
// $.each(cidades, function (indice, cidade) {
// alert(cidade.Nome + ": " + cidade.Uf);
// });
}
});
有人有一些想法?
问题是跨端脚本。这可以通过使用'jsonp'
作为dataType很容易地解决。 然后改变网站使用的是已被添加到该呼叫““?回调=”和包装,围绕JSON。例如,如果callback = 'test'
test('[{"Nome":"AGUAS DE LINDOIA","Uf":"SP"},{"Nome":"AMERICANA","Uf":"SP"},{"Nome":"AMPARO","Uf":"SP"}]')
朋友,真的很感谢你!因为我使用WCF 4,我只需要更改我的web.config接受跨端([链接] http://stackoverflow.com/questions/1178614/return-json-wrapped-in-a-callback-function-from-a-wcf-rest-web- service [/ link]),当然也可以使用jsonp。再一次,谢谢! – quicoli
Link for the descriptionstatus === 0
- XHR状态,是初始化或没有,这意味着用户处于脱机状态。
在执行跨站点脚本(访问被拒绝)或请求无法访问的URL(错字,DNS问题等)时,您会看到状态为0。
用户必须使用JSONP的。基本上这是包装返回的JSON在一个函数调用的方式 – RvdK
请记住,如果调用跨域你有使用jsonp! – RvdK
是的,你的是正确的 – quicoli