EDIT: It seems that the formatting of the json string is not the problem. Instead I’m not seeing the json string in the Firebug response section and yet ajax error handlers aren’t being thrown.
Hi!
So I’ve got a web service setup so that it will return a very simple json string:
{"A":1,"Total":1}
After reading several threads on stackoverflow and even on the jquery documentation page for $.ajax, I learned that the correct formatting for the json string is to have each value within double quotes as well.
{"A":"1","Total":"1"}
I’m assuming that is why my jquery ajax is not working:
$.ajax({
type: "GET",
url: "http://test/testServices/InfoService.svc/GetInfoStats?pId=76&mId=2117",
dataType: "json",
error: function(xml,txtStatus, errorThrown){
alert(xml + " " + txtStatus + " " + errorThrown);
},
success: function (msg, test) {
alert("JSON DATA LOADED! " + msg);
$(".MainContainerBottom").html(msg);
}
});
$('.log').ajaxError(function () {
$(this).text('Triggered ajaxError handler.');
});
Firebug’s Console window is telling me that the GET was “200 OK 398ms” consistently and if I check test variable it returns success as well. msn returns null though!
The Params and the Headers section of Firebug also looks good but the Response section is empty.
I was also messing around with the dataType of the $.ajax and had it as jsonp at first and firebug returned an error stating
Invalid Label
{"A":1,"Total":1}
So I know the information is coming back correctly but I can’t access it. I’m assuming it’s because of the formatting issue I began this post with. Is that correct?
Assuming I don’t have the ability to modify the web service myself, what can I do to get the json data out and into my script?
Thank you very much! <3
John
EDIT: For Guffa
Response Headers
Content-Length 39
Content-Type application/json; charset=utf-8
Server Microsoft-IIS/7.5
X-Powered-By ASP.NET
Date Fri, 12 Nov 2010 21:58:57 GMT
Request Headers
Host test
User-Agent Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.9) Gecko/20100824 Firefox/3.6.9
Accept application/json, text/javascript, */*
Accept-Language en-us,en;q=0.5
Accept-Encoding gzip,deflate
Accept-Charset ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive 115
Connection keep-alive
Final Edit:
I spoke to the guy who setup the web service for us and apparently there was some special restrictions on the information being sent back. He setup some permissions for our application to access the data and it worked flawlessly.
Thanks to everyone for your help! I’m really surprised by the amount of people who responded and tried to help me out. I’ll definitely be sticking around the stackoverflow community now and hopefully one day I can help out others as well 🙂
No, there is nothing wrong with the original JSON string. Values doesn’t have to be strings, they can be numbers.
You have to look for the error elsewhere. Add an error handler in the Ajax call, so that you can get any information about what might be going wrong.
When you fetch data as JSONP, it expects the result wrapped in a callback call:
callback({"A":1,"Total":1});. The code is executed when it’s returned, and as there isn’t a callback call around the object, the brackets around the object literal is instead interpreted as a code block, and the property names are interpreted as code labels. Code labels have to be identifiers, not strings, hence the error message.