Thursday, July 2, 2009

jQuery's getJSON

I recently had a struggle getting jQuery's $.getJSON function to work on Internet Explorer (IE6 & IE7). I tried and tried to debug the javascript, but was getting no where. After giving up for a while and coming back to it the next day, I thought of checking the target file—the file jQuery was fetching asynchronously—for problems.

It turns out that IE doesn't like JSON encoded documents that are specified as UTF-8. Not sure what the problem is, but the document has to be returned in a format IE likes. If not, IE will not allow jQuery to process it. Now, I had a header on this file that specified the content-type and charset. But, I changed it and instead I put only the content-type at the top of my JSON-returning PHP files:

<?PHP
header("Content-Type: application/json");
?>

Why does IE balk at UTF-8 specified charsets for JSON-encoded responses?? Who knows. But, from the research I've done, specifying the charset in the HTTP header is not something that is required. I am sure there are good reasons to specify the charset, but this (above) works great for me.

6 comments:

Marcos JF Ramos said...

Man, u save my life, tnx!!

Unknown said...

Thanks so much for taking the time to post this, you saved me many hours!

Maybe its time for a class action lawsuit against M$ for all the hours programmers around the world lost due their software not being standards compliant!

Eric said...

No problem.

I just don't get why they would create a browser that doesn't accept UTF-8 AJAX queries. UTF-8 should be the norm!

Anonymous said...

Nice. Did the trick.

beno said...

Hi,

Thanks for your post. I've being wrestling with this a lot.

And I am still...

The content does not get loaded in the first page load, it loads if I refresh the page.

Well, before your suggestion IE did nothing....

I hate IE...

Goldan said...

Thank you, the solution actually works for me, unlike the others I've found over the Internet.