3

I am using jQuery.getJSON to fetch the Facebook friends list, but I am not getting it. It works perfectly in Firefox and Chrome, but it is not working in Internet Explorer 8.

jQuery.getJSON("https://graph.facebook.com/me/friends?access_token="+aToken,
    function(data) {
        alert(data);
    }
);

Also after doing a little more research I tried with this code also:

jQuery.ajax({
    url:"https://graph.facebook.com/me/friends?access_token="+aToken,
    type: 'json',
    success: function(json) {
        alert(json);
    }
});
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Nathan Sri
  • 103
  • 1
  • 4
  • 14

3 Answers3

8

Internet Explorer 8 doesn't support CORS in the XMLHttpRequest object which jQuery is using. Internet Explorer 8 uses XDomainRequest object which jQuery doesn't support by default.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Esailija
  • 138,174
  • 23
  • 272
  • 326
  • 1
    then how to fix it is there any hack for it? – Nathan Sri Dec 10 '11 at 13:18
  • @Esailija I thought $.getJSON just did a JSONP request on a GET if it can't use CORS? – isNaN1247 Dec 10 '11 at 13:19
  • 3
    @beardtwizzle, getJSON is just `$.ajax` with `dataType: "json"`, it's not JSONP which is not ajax type of request at all. JSONP exists in jQuery.ajax though with type jsonp or with `getJSON` when the string contains `"callback=?"`. The facebook graph url as is in the op, doesn't send JSONP response anyway. – Esailija Dec 10 '11 at 13:20
  • Ah yes... its the latter part that I was thinking of (i.e. when you append the callback querystring) – isNaN1247 Dec 10 '11 at 13:26
  • @beardtwizzle, yes it's the stupid overloads like that which make me rage at jQuery every day =) – Esailija Dec 10 '11 at 13:28
  • Here is a plugin that adds XDomainRequest support to JQuery: https://github.com/jaubourg/ajaxHooks/blob/master/src/ajax/xdr.js – monsur Jan 02 '12 at 02:40
7

Try this to handle the error:

jQuery.getJSON("https://graph.facebook.com/me/friends?access_token=" + aToken, 
    function(data) {
        alert(data);
    }
)
.error(function(jqXHR, textStatus, errorThrown) { alert(errorThrown); });

And try this hack in your code (as per comment below)

jQuery.support.cors = true;
ndsmyter
  • 6,535
  • 3
  • 22
  • 37
leomeurer
  • 742
  • 1
  • 8
  • 24
  • 1
    Try this hack: Put jQuery.support.cors = true; in your code. – leomeurer Dec 10 '11 at 13:39
  • @Nathan - For clarity, was the actual answer adding 'jQuery.support.cors' in the code? – isNaN1247 Dec 10 '11 at 14:09
  • I have the same error in IE8 with Jquery 1.7.2, got "access is denied" but adding jQuery.support.cors = true; doesn't help. Am I doing something wrong? The code works perfectly in IE10+ – pomarc Jun 11 '14 at 06:23
  • @pomarc, This piece of code only shows what kind of error you are receiving, and it will help you to solve the real problem. – leomeurer Jun 11 '14 at 21:47
3

I totally solved this problem using Jason Moon script, here

https://github.com/MoonScript/jQuery-ajaxTransport-XDomainRequest/blob/master/jQuery.XDomainRequest.js

hope it helps.

pomarc
  • 2,194
  • 3
  • 23
  • 30