пытаясь получить количество лайков с фан-страницы Facebook, используя JSONP и JQuery - PullRequest
1 голос
/ 25 августа 2011

Итак, я пытаюсь вытащить и напечатать только количество «лайков», которые есть у определенной фан-страницы, после некоторого исследования - я обнаружил, что это должно работать, но это ничего не тянет. Любая помощь?

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>

<script type="text/javascript">

function fbFetch(){
    //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/tenniswarehouse?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>" + likes + "</li>
                    </ul>";

        //A little animation once fetched
        $('.facebookfeed').animate({opacity:0}, 500, function(){
            $('.facebookfeed').html(html);
        });

        $('.facebookfeed').animate({opacity:1}, 500);
    });
};

</script>
</head>
<body onload="fbFetch()">
    <div class="facebookfeed">
        <h2>Loading...</h2>
    </div>
</body>
</html>

Ответы [ 2 ]

5 голосов
/ 25 августа 2011

Я получаю ошибку javascript в консоли. Попробуйте запустить функцию готового документа jquery вместо тела onLoad. Также, likes не определено, это должен быть json.likes.

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/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/tenniswarehouse?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></ul>";
        //A little animation once fetched
        $('.facebookfeed').animate({opacity:0}, 500, function(){
            $('.facebookfeed').html(html);
        });
        $('.facebookfeed').animate({opacity:1}, 500);
    });
  });
</script>
</head>
<body>
    <div class="facebookfeed">
        <h2>Loading...</h2>
    </div>
</body>
</html>
0 голосов
/ 25 августа 2011

Вы пытались вместо этого назвать URL по его идентификатору?Также здесь применяется тот же протокол https / http.Возможно, вам потребуется вызвать URL с помощью document.location.protocol, чтобы соответствовать текущему протоколу вызывающего документа.

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>

<script type="text/javascript">

function fbFetch(){
    //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 = document.location.protocol + "//graph.facebook.com/tenniswarehouse?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>" + likes + "</li>
                    </ul>";

        //A little animation once fetched
        $('.facebookfeed').animate({opacity:0}, 500, function(){
            $('.facebookfeed').html(html);
        });

        $('.facebookfeed').animate({opacity:1}, 500);
    });
};

</script>
</head>
<body onload="fbFetch()">
    <div class="facebookfeed">
        <h2>Loading...</h2>
    </div>
</body>
</html>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...