2012-03-06 66 views
0

我有一个JavaScript函数用于调用Facebook API并获取来自墙上的帖子列表。在Firefox,Chrome和Safari这个没有问题,但在互联网 资源管理器  9(我还没有测试下面),它只是没有做任何事情,UI保持阻止,但我似乎并没有得到任何问题指向错误的消息。我有一种感觉,它可能与正在返回的JSON和互联网  Explorer解析器有关,但在这一点上我无法确定。Facebook Graph Call在Internet Explorer 9中不起作用

在被重新指向Facebook SDK之后,我重新实现了这个功能(我不知道为什么现在我要离开它),并且它现在似乎都可以很好地与Internet   Explorer配合使用。

旧代码

var getFeed = function (name, type, limit, accessToken, apiKey, containerId) { 
    var list_url = "https://graph.facebook.com/" + name + "/" + type + "?limit=" + limit + "&access_token=" + accessToken + "&api_key=" + apiKey; 
    var html = ""; 

    displayHelper.blockUI(containerId, "Loading Feed"); 
    $.getJSON(list_url, function (json) { 
     //Loop through and within the data array to retrieve the variables. 
     $.each(json.data, function (i, fb) { 
      var msg = (typeof (fb.message) != "undefined") ? fb.message : ""; 
      var link = (typeof (fb.link) != "undefined") ? fb.link : ""; 
      var pic = ""; 
      // msg = (typeof(fb.story) != "undefined") ? fb.story : msg; 
      var type = (typeof (fb.type) != "undefined") ? fb.type : ""; 
      var includeInOutput = true; 

      pic = getPicture(fb.from.id); 

      switch (type) { 
       case "story": 
        msg = fb.story; 
        break; 
       case "link": 
        if (typeof (fb.message) != "undefined") 
         msg = fb.message; 
        else if (typeof (fb.caption) != "undefined") 
         msg = fb.caption; 
        else if (typeof (fb.story) != "undefined") 
         msg = fb.story; 
        else if (typeof (fb.name) != "undefined") 
         msg = fb.name; 
        break; 
       case "video": 
       case "photo": 
        if (typeof (fb.message) != "undefined") 
         msg = fb.message; 
        else if (typeof (fb.story) != "undefined") 
         msg = fb.story; 
        break; 
       case "status": 
        if (typeof (fb.message) != "undefined") 
         msg = fb.message; 
        else if (typeof (fb.story) != "undefined") 
         msg = fb.story; 
        break; 
       default: 
        includeInOutput = false; 
        break; 
      } 

      if (includeInOutput) { 
       //build html for this list item 
       html += '<dl class="fb-item">'; 
       html += "<dt>" + fb.from.name + "</dt>"; 
       html += (pic != '') ? '<dd class="img"><img src="' + pic + '" />' : ''; 
       html += '<dd class="msg">' + msg + '</dd>'; 

       /*html += '<a href="' + link + '" class="fb_link" target="_blank">' 
       + msg + "(" + type + ")" 
       + "</a>";*/ 

       html += '<dd class="links">'; 
       html += '<span>' + fuzzyTime(fb.created_time.replace(/-/g, '/')) + '</span>'; 

       if (typeof (fb.actions) != "undefined") { 
        if (fb.actions[1].name == "Like") 
         html += "<a href='" + fb.actions[1].link + "' class='fb_link' target='_blank'>Like</a> - "; 

        if (fb.actions[0].name == "Comment") 
         html += "<a href='" + fb.actions[0].link + "' class='fb_link' target='_blank'>Comment</a>"; 
       } 
       html += '</dd>'; 
       html += "</dl>"; 
      } 

     }); /* end .each */ 

     //html += "</ul>"; 
     $(containerId).html(html); 
     $(containerId).unblock(); 

    }); /* end getJSON */ 

} /* end hetFeed 

新代码 - 再次更新。图片没有返回,因此我提取了消息并将其构建到自己的方法中,并在获取图片的回调中构建了消息。在我以阻塞的方式进行之前,错了!希望这有助于某一天。

var postMessage = function (fb, containerId) { 
    FB.api("/" + fb.from.id + "/?fields=picture", {}, function (p) { 
     var pic = p.picture; 
     var msg = (typeof (fb.message) != "undefined") ? fb.message : ""; 
     var link = (typeof (fb.link) != "undefined") ? fb.link : ""; 
     var type = (typeof (fb.type) != "undefined") ? fb.type : ""; 
     var includeInOutput = true; 
     var html = ""; 

     switch (type) { 
      case "story": 
       msg = fb.story; 
       break; 
      case "link": 
       if (typeof (fb.message) != "undefined") 
        msg = fb.message; 
       else if (typeof (fb.caption) != "undefined") 
        msg = fb.caption; 
       else if (typeof (fb.story) != "undefined") 
        msg = fb.story; 
       else if (typeof (fb.name) != "undefined") 
        msg = fb.name; 
       break; 
      case "video": 
      case "photo": 
       if (typeof (fb.message) != "undefined") 
        msg = fb.message; 
       else if (typeof (fb.story) != "undefined") 
        msg = fb.story; 
       break; 
      case "status": 
       if (typeof (fb.message) != "undefined") 
        msg = fb.message; 
       else if (typeof (fb.story) != "undefined") 
        msg = fb.story; 
       break; 
      default: 
       includeInOutput = false; 
       break; 
     } 

     if (includeInOutput) { 
      //build html for this list item 
      html += '<dl class="fb-item">'; 
      html += "<dt>" + fb.from.name + "</dt>"; 
      html += (pic != '') ? '<dd class="img"><img src="' + pic + '" />' : ''; 
      html += '<dd class="msg">' + msg + '</dd>'; 

      /*html += '<a href="' + link + '" class="fb_link" target="_blank">' 
      + msg + "(" + type + ")" 
      + "</a>";*/ 

      html += '<dd class="links">'; 
      html += '<span>' + fuzzyTime(fb.created_time.replace(/-/g, '/')) + '</span>'; 

      if (typeof (fb.actions) != "undefined") { 
       if (fb.actions[1].name == "Like") 
        html += "<a href='" + fb.actions[1].link + "' class='fb_link' target='_blank'>Like</a> - "; 

       if (fb.actions[0].name == "Comment") 
        html += "<a href='" + fb.actions[0].link + "' class='fb_link' target='_blank'>Comment</a>"; 
      } 
      html += '</dd>'; 
      html += "</dl>"; 
     } 

     $(containerId).append(html); 
    }); 
} 

var getFeed = function (name, type, limit, accessToken, apiKey, containerId) { 

    var list_url = "https://graph.facebook.com/" + name + "/" + type + "?limit=" + limit + "&access_token=" + accessToken + "&api_key=" + apiKey; 
    var fullHtml = ""; 

    helper.blockUI(containerId, "Loading Feed"); 
    var path = "/" + name + "/" + type; 

    FB.api(path, { access_token: accessToken, api_key: apiKey, limit: limit }, function (json) { 
     console.log(json); 
     var data = json.data; 
     for (var i = 0, l = data.length; i < l; i++) { 
      var fb = data[i]; 

      postMessage(fb, containerId); 

     } //End For 

     $(containerId).unblock(); 
    }); 

} /* End getFeed */ 
+1

不回答你的问题,但我很好奇你为什么不使用[facebook javascript sdk](http://developers.facebook.com/docs/reference/javascript/)。它会照顾添加所需的参数并为您发出ajax调用,因为您没有使用它? – 2012-03-06 20:02:44

+0

老实说,我一直在努力的API和发现类似的代码只是为了让它移动。 – Modika 2012-03-07 06:13:29

回答

1

无论如何,您可能想尝试使用sdk,这样您可以更好地知道IE中代码的问题到底在哪里。

如果你这样做:

var path = name + "/" + type; 
var params = limit == null ? {} : { limit: limit }; 

FB.api(path, "post", params, function(json) { 
    ...... 
}); 

应该几乎一样做你发布的代码(减去结果的实际处理)。如果你尝试它,并且这在IE中起作用,那么问题出在你如何与Facebook进行交流,否则它将处理来自Facebook的结果。

有一点可以肯定,这种方法(使用sdk)更简单和更优雅,更不用说,通过这种方式,facebook所做的改变不会影响你(在大多数情况下)。

+0

接受这个问题后,我发表了你的文章,我修改了SDK,并且获得了大部分的启动和运行,并在IE9中进行了测试,并且似乎又回来了。将完成后发布代码,谢谢指出我在正确的方向。 – Modika 2012-03-07 13:33:30