Sam
Sam

Reputation: 5526

jQuery AJAX request failing in IE

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

Upvotes: 38

Views: 57521

Answers (8)

Sam
Sam

Reputation: 5526

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

Upvotes: 51

Jav_Rock
Jav_Rock

Reputation: 22245

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
});

Upvotes: 7

user333483
user333483

Reputation: 1

IE: JSON not defined error resolved at

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

by using dataType: "json" and avoid parsing

Upvotes: 0

tanathos
tanathos

Reputation: 5606

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

$.ajax({ 
    url: "yoururl",
    cache: false,
    ....

Upvotes: 48

Luca Matteis
Luca Matteis

Reputation: 29267

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.

Upvotes: 2

Craig Stuntz
Craig Stuntz

Reputation: 126547

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.

Upvotes: 4

Pim Jager
Pim Jager

Reputation: 32119

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?

Upvotes: 0

Javier
Javier

Reputation: 62583

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.

Upvotes: 8

Related Questions