如何使用图形api ajax调用获取#facebook评论

时间:2012-05-24 17:49:34

标签: ajax facebook json

var url = 'https://graph.facebook.com/?ids=http://www.stackoverflow.com';

$.getJSON(url, function(resp) {
  $("p").html('comments = ' + resp.comments);
});

这会将以下html放在我的<p>comments = undefined

我从通话中得到的返回字符串是:

{
   "http://www.thinlinebetween.com/tuesday-quote-of-the-day-jean-ingelow/": {
      "id": "http://www.thinlinebetween.com/tuesday-quote-of-the-day-jean-ingelow/",
      "shares": 2,
      "comments": 1
   }
}

我是否需要执行resp."http://www.thinlinebetween.com/tuesday-quote-of-the-day-jean-ingelow/".comments之类的操作才能获得价值?返回的json不容易解析。谢谢!

2 个答案:

答案 0 :(得分:0)

var postUrl = 'http://www.stackoverflow.com';
var apiUrl = 'https://graph.facebook.com/?ids=' + postUrl;

$.getJSON(apiUrl, function(resp) {
  $("p").html('comments = ' + resp[postUrl].comments);
});

需要resp["http://www.stackoverflow.com"].comments

答案 1 :(得分:0)

这应该是方式,

$(function(){ 
        var webUrl = 'http://www.stackoverflow.com';
        var endpoint = 'https://graph.facebook.com';
        $.getJSON(endpoint,{'ids':webUrl},function(response){
            var comments = response[webUrl].comments;
              $("p").html('comments = ' + comments);
        })
    });