我一直在寻找一种facebook的json方法来获取给定jQuery的统计数据,这是我在uni中开始的一个项目。我发现这里已经有人问过了:trying to pull the number of likes from a facebook fanpage using JSONP and JQuery
在我的页面上使用了代码后,它工作得很好,而且是轻量级的,但是我现在想使用这个代码从多个页面中拉出结果,但我试图找到一个解决方案,但却碰壁了。
我当前的代码,拉入一个页面的数据是:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
//Set Url of JSON data from the facebook graph api. make sure callback is set with a '?' to overcome the cross domain problems with JSON
var url = "https://graph.facebook.com/immbudden?callback=?";
//Use jQuery getJSON method to fetch the data from the url and then create our unordered list with the relevant data.
$.getJSON(url,function(json){
var html = "<ul><li>" + json.likes + "</li><li>" + json.about + "</li></ul>";
//A little animation once fetched
$('.facebookfeed').animate({opacity:0}, 500, function(){
$('.facebookfeed').html(html);
});
$('.facebookfeed').animate({opacity:1}, 500);
});
});
</script>
<link rel="stylesheet" href="style.css" media="all">
</head>
<body>
<div id="wrapper"><!--wrapper open-->
<div class="facebookfeed">
<h2>Loading...</h2>
</div>
</div><!--wrapper closed-->
</body>
从字面上讲,我刚刚开始适当地钻研jQuery,所以任何帮助都是非常感谢的!
提前谢谢你,迈克尔
发布于 2012-02-06 12:42:52
您可以批量请求Facebook API,这样您甚至不需要对多个页面进行多次调用:)
批处理请求类似于:https://graph.facebook.com/?ids=id1,id2,id3
等。然后,您需要遍历结果以将其打印出来。因此,整个更改将如下所示:
var url = "https://graph.facebook.com/?ids=immbudden,page2,page3&callback=?";
$.getJSON(url, function(json) {
var html = '';
$.each(json, function(index, item) {
html += "<ul><li>" + item.likes + "</li><li>" + item.about + "</li></ul>";
});
//A little animation once fetched
$('.facebookfeed').animate({
opacity: 0
}, 500, function() {
$('.facebookfeed').html(html);
});
$('.facebookfeed').animate({
opacity: 1
}, 500);
});
我在这里为您创建了一个有效的jsFiddle:http://jsfiddle.net/rYyzf/
https://stackoverflow.com/questions/9154925
复制相似问题