38

The following AJAX call is failing in IE.

$.ajax({
    url:"{{SITE_URL}}/content/twitter.json",
    dataType:"json",
    error:function(xhr, status, errorThrown) {
        alert(errorThrown+'\n'+status+'\n'+xhr.statusText);
    },
    success:function(json) {
               ...Snip...
    }
});

The error function returns

Undefined
parsererror
OK

No request is made to the server so I don't think its a problem with the JSON.

Fixed, See #1351389

informatik01
  • 16,038
  • 10
  • 74
  • 104
Sam
  • 5,416
  • 7
  • 47
  • 49

8 Answers8

51

Fixed, I changed the content-type from application/json; charset=utf8 to just plain application/json.
I hate IE :)

Also to avoid IE super-caching try this:

var d = new Date();
$.ajax({
        url:"{{SITE_URL}}/content/twitter.json?_="+d.getTime(), 
...Snip...

That way each request is a new url for IE to get :D

informatik01
  • 16,038
  • 10
  • 74
  • 104
Sam
  • 5,416
  • 7
  • 47
  • 49
  • 27
    God, am I glad I found this answer. This thing has cost me days of my life that Microsoft won't give me back. Just to reiterate: I effing hate IE, too. – Leonard Ehrenfried Nov 10 '09 at 21:11
  • 14
    Dear IE, I hate you. Dear @Sam, I love you. – Gabe Apr 26 '10 at 15:10
  • 1
    Also possible with jQuery.Ajax option "cache". http://api.jquery.com/jQuery.ajax/#options If set to false, it will force requested pages not to be cached by the browser. Setting cache to false also appends a query string parameter, "_=[TIMESTAMP]", to the URL. – John Korsnes Feb 02 '12 at 14:50
  • why set content-type just to `application/json`. is work? can you explain this..? – viyancs Mar 15 '12 at 02:44
  • 1
    Ie was getting hung up on the charset declaration – Sam Mar 15 '12 at 09:10
  • 1
    Thx for a solution! +1 for all IE haters – Vlad Dneprovskiy Apr 04 '12 at 10:34
  • Sam, I don't know who you are, but I think I love you. I spent over a day trying to figure out why IE wouldn't parse the JSON. Nowhere on the internet does it say that IE fails to do so if you include the charset. – Tommy Brunn Apr 12 '12 at 09:01
  • This also helps resolve every element on the page being selected if the AJAX call was aborted (not a success or error) – Ben Petersen Jun 11 '12 at 18:50
49

For the caching problem why don't you simple use the cache: false parameter?

$.ajax({ 
    url: "yoururl",
    cache: false,
    ....
tanathos
  • 5,566
  • 4
  • 34
  • 46
8

is this a copy/paste? the one thing that gets me all the time is leaving the last ',' in an object constructor. that is, most browsers JS accept:

o = { a:1, b:2, c:3, };

but IE chokes on this because the comma after the last item. change it to:

o = { a:1, b:2, c:3 };

and it works.

Javier
  • 60,510
  • 8
  • 78
  • 126
  • Men - you are a freak :) You saved me alot time. Note that this breaks only IE7 and below. From IE8 this isn't rise errors. – bksi May 29 '12 at 00:12
7

In newer versions of internet explorer (IE7) it is necessary to write the next line before calling $.ajax, otherwise it would never call the function:

$.ajaxSetup({ cache: false }); //this line before $.ajax!!!
$.ajax({
    //codes
    //codes
    //codes
});
Jav_Rock
  • 22,059
  • 20
  • 123
  • 164
4

IE caches AJAX requests really aggressively (more so than Firefox, anyway). You need to set the Cache-Control headers in the response appropriately if this is not right for your site.

Craig Stuntz
  • 125,891
  • 12
  • 252
  • 273
  • 1
    Good point, ive added ?_=1234 on the end of it and ie now request the page every time. Still same problem though :( – Sam Jan 08 '09 at 22:14
2

One major problem with statically generated JSON and IE are the leading "commas", for examples this throws an error in IE:

{
    "one":"hello",
    "two":"hi",
 }

Note the last comma.

Luca Matteis
  • 29,161
  • 19
  • 114
  • 169
0

IE: JSON not defined error resolved at

http://funkatron.com/site/comments/safely-parsing-json-in-javascript/

by using dataType: "json" and avoid parsing

0

What is the {{SITE_URL}} chunk giving is about. Try looking at the code in view source code of the browser. If the {{SITE _URL}} chunk has a trailing slash and that would make the request url:

http://modomain.com//content/twitter.json

Which could creep IE out?

Pim Jager
  • 31,965
  • 17
  • 72
  • 98