8

I have been trying to solve this problem for hours (searched here as well but none of the solutions worked) so I had no other option but to hope for someone to tell me why this is happening and how can I fix it.

This is a simple code that works with Firefox but not with IE9 (don't have other versions)

Example code is here:

http://jsfiddle.net/z5b2J/

Source is this one:

$.ajax({
  url: "http://query.yahooapis.com/v1/public/yql?q=select%20script%20from%20html%20where%20url%3D%27https%3A%2F%2Ftesting.website.com%2F%3Fcid%3D48hgfd45430DD%26id%3D4830F8CF0454312%27&format=json&diagnostics=true&_maxage=86400",
  success: function(){
   alert('hi');
  }
});

The website doesn't need to be real for testing purpose.

As you can see in the fiddle under Firefox, an alertbox appears saying "hi" BUT if you run the exact same code in IE9, the alertbox doesn't appear.

The same situation occurs with getJSON method, this is a problem for me because I want to run some code instead the alert but it won't run in IE9.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
MikeJ
  • 83
  • 1
  • 1
  • 3

5 Answers5

12

Did you try using getJSON() instead of ajax? This is a cross-domain request and you're fetching json so that it probably the problem.

It's working in both browsers now:

$.getJSON("http://query.yahooapis.com/v1/public/yql?q=select%20script%20from%20html%20where%20url%3D%27https%3A%2F%2Ftesting.website.com%2F%3Fcid%3D48hgfd45430DD%26id%3D4830F8CF0454312%27&format=json&diagnostics=true&_maxage=86400&callback=?",function(){
   alert('hi');
});
citizen conn
  • 15,300
  • 3
  • 58
  • 80
  • "The same situation occurs with getJSON method" Yes I did try. – MikeJ Jun 29 '11 at 00:16
  • 8
    (remember you have to add the "&callback=?" argument to the URL... this is one way, or you can specify 'jsonp' as the datatype in your $ajax call. – citizen conn Jun 29 '11 at 00:22
  • Perfect, do you know why it needs that argument? – MikeJ Jun 29 '11 at 00:28
  • 1
    that argument just means it's going to be a cross-browser jsonp call. jquery official docs say: "If the URL includes the string "callback=?" (or similar, as defined by the server-side API), the request is treated as JSONP instead. See the discussion of the jsonp data type in $.ajax() for more details." – citizen conn Jun 29 '11 at 00:32
6

The problem of IE9 depends on advanced cache management.

If you empty the cache of IE and re-run the ajax request: the first time will work.

To resolve this problem you must send your HTTP response with "noStore = true" and "Duration = 0" or equivalent.

Here is an example in MVC.

Code Lღver
  • 15,573
  • 16
  • 56
  • 75
bruce0wayne
  • 61
  • 1
  • 1
4

Using $.getJSON or $.ajax you must also specify dataType parameter 'jsonp'

Here the example using getJSON:

var webpage = ".... your very long url ....";
var anchor = document.createElement('a');
anchor.href = webpage;
// handle the multiple parameters
anchor.search += ((anchor.search.length > 0) ? "&" : "?");
anchor.search += "callback=?";

$.getJSON(anchor.href, 'jsonp',  function(data, textStatus, jqXHR){
   alert('hi');
});
freedev
  • 25,946
  • 8
  • 108
  • 125
2

I solved problem by adding "Duration=0" with the url

lokeshgopu
  • 21
  • 2
2

If you add callback=? to the URL, it will be transformed to something like callback=jQuery1820719005049791166_1366033695001 . It is the name of the function and on the server side you should wrap your json encoded object in this function call. So your response body should be not just {ok: true}, but jQuery1820719005049791166_1366033695001({ok: true}); . It worked for me!