Friends, I’m lost here.
I have this WCF Rest service returning data in json format: http://189.126.109.249/ieptb/Cidades?uf=SP
I can access it using a asp.net web form application and also, I can access it using a Windows Phone application. But I’m not getting to make it work under a simple jQuery.$Ajax() call. My jQuery is always returning an error. If you take a look into my code you will see I have a function for catching errors.
Here is the script I’m using: http://jsfiddle.net/n6sLQ/4/
I’ve tested with Fiddler, and it shows me that HTTP response is 200 (ok), even it shows me the json array returned, like this:
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"}]
I dont know what is wrong with my 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);
// });
}
});
Someone has some idea?
The problem is cross-side scripting. This can easily fixed by using
'jsonp'as dataType.And then changing the website to use the ‘”?callback=’ that has been added to the call and wrap that around the json. For example if
callback = 'test'