首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用json为多个页面引入facebook点赞

使用json为多个页面引入facebook点赞
EN

Stack Overflow用户
提问于 2012-02-06 10:14:04
回答 1查看 2.1K关注 0票数 1

我一直在寻找一种facebook的json方法来获取给定jQuery的统计数据,这是我在uni中开始的一个项目。我发现这里已经有人问过了:trying to pull the number of likes from a facebook fanpage using JSONP and JQuery

在我的页面上使用了代码后,它工作得很好,而且是轻量级的,但是我现在想使用这个代码从多个页面中拉出结果,但我试图找到一个解决方案,但却碰壁了。

我当前的代码,拉入一个页面的数据是:

代码语言:javascript
运行
复制
<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,所以任何帮助都是非常感谢的!

提前谢谢你,迈克尔

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-02-06 12:42:52

您可以批量请求Facebook API,这样您甚至不需要对多个页面进行多次调用:)

批处理请求类似于:https://graph.facebook.com/?ids=id1,id2,id3等。然后,您需要遍历结果以将其打印出来。因此,整个更改将如下所示:

代码语言:javascript
运行
复制
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/

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9154925

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档