Madhan
Madhan

Reputation: 506

Ajax Requests to Facebook graph not working on IE

I am developing a facebook app. I am trying to get the likes for urls as I am iterating through an array of urls and adding it to the div that should contain the likes. This is my ajax request:

 $.ajax({
                  type: "GET",
                  cache: false,
                  url: 'https://graph.facebook.com/?id='+videoUrl,
                  dataType : "json",
                  async: false,
                  header: "application/json",
                  success: function(data) {
                      if(data.shares == null) {
                          likeCount = 0;
                      } else {
                          likeCount = data.shares;
                      }
                  }
          });

This works perfectly fine on Chrome, Firefox, Safari. It doesn't work on any IE. I thought it was a caching issue. Notice that I added cache: false to the request. I tried JSONP and I get the likes but I am unable to access it outside of the request. I also tried adding this to my php:

    header("Cache-Control: post-check=0, pre-check=0", false);

It still doesn't work. Please help me!

Upvotes: 1

Views: 1467

Answers (2)

Sean Kinsey
Sean Kinsey

Reputation: 38046

This is because IE's support for CORS is quite bad and so wil only work in certain scenarios. For best support you should use Facebook's FB.api via the JS SDK.

Upvotes: 0

urban_raccoons
urban_raccoons

Reputation: 3499

I was having a similar problem. This worked for me:

$.get('https://graph.facebook.com/?id='+videoUrl+'&callback=?', function(data) {
                  if(data.shares == null) {
                      likeCount = 0;
                  } else {
                      likeCount = data.shares;
                  }
              }, 'json');

The "&callback=?" in the url causes jQuery to treat it as JSONP. See What are the differences between JSON and JSONP? for a much more detailed explanation than I could provide here.

Upvotes: 4

Related Questions