« Back to blog

jQuery 1.4.2 and Ajax

Ok, finally figured out a huge problem that I was having with jQuery 1.4.2 and all my callback functions that were returning JSON data: you need to have it formatted in a very very strict manner. Otherwise, jQuery will not place nice. I was able to get the answers from looking through the comments on jQuery.post() [http://api.jquery.com/jQuery.post/].

In a nutshell, for jQuery 1.4.2, this JSON format is works: { "key1" : "value1", "key2" : "value2"}

But this doesn't: { 'key1' : 'value1'} (notice the single quote)

Nor: { key1 : 'value1 }

Here's how I figure it out from a user's comment on the jquery site:

And remember that they must be double-quotes, not single. Also: Firebug's Net tab is lenient and will show its JSON tab even if the JSON is not 100% correct.

It was hard to debug malformed JSON issues because the API doc says "We are now strict about incoming JSON and throw an exception if we get malformed JSON." Since post is ansychronous, it's not clear to me in what context the exception is thrown. All I could see is that my callback wasn't being called and luckily I had read the 1.4 release notes and remembered the comment about JSON. How could I have detected this more quickly?