Ajax Requests to Facebook graph not working on IE

送分小仙女□ 提交于 2019-12-11 06:28:32

问题


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!


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/12268496/ajax-requests-to-facebook-graph-not-working-on-ie

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!