2012-02-06 7 views
1

私はuniで始まっているプロジェクトのために、特定のFacebookの統計情報を取得するjQuery jsonメソッドを探しています。 trying to pull the number of likes from a facebook fanpage using JSONP and JQueryjsonを使用して複数のページのfacebook好きを引き出す

私のページにコードを使用していて、うまく動作し、軽量ですが、このコードを使用して複数のページから結果を取り込みたいのですが、解決策を見つけようとし、レンガの壁に当たった。

私の現在のコード、1ページのデータを引き込むことである:私は

<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を掘り下げるために始めていますので、すべての任意のヘルプは大歓迎です!事前に

おかげで、マイケル

答えて

3

することはできバッチ要求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/

+0

ああ、優れた:)感謝を! 1つの質問ですが、各コールバックを使用する方法では、それぞれのIDがそれぞれのdivに表示され、それぞれのfacebookページのイラストやその他の情報が含まれます。このメソッドはこれを許可しますか?もう一度ありがとう:) – Michael

+0

もちろん、各項目をそれ自身のdivとフォーマットに入れてループを編集するだけです:) –

+0

私は今日、指定されたdivに各項目を追加しようとしていますが、正しく機能していないようですここでは、私が試したコードのjsFiddleです:http://jsfiddle.net/immbudden/PRA7b/ 私はおそらく何か愚かなことをやっているが、私のjQueryを読んでそれを動作させるように見えない本や学習のように事前に謝罪しています。 – Michael

関連する問題