2011-08-25 5 views
1

일부 연구를 수행 한 후 특정 팬 페이지에있는 "좋아하는"수만 인쇄하려고합니다. ,하지만 아무것도 당기지 않습니다. 어떤 도움이 필요합니까?JSONP 및 JQuery를 사용하여 facebook 팬 페이지에서 좋아하는 수를 가져 오려고 시도합니다.

<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> 

답변

5

콘솔에서 자바 스크립트 오류가 발생합니다. 본문 onLoad 대신 jquery 문서 준비 기능을 실행하십시오. 또한 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

여전히 작동하지 않습니다. - 그 콜백 =? 다른 도메인 (페이스 북) –

+0

에서 JSON을 가져 오기 때문에 내 업데이트를 확인하기 때문입니다. – bkaid

+0

놀라운 감사합니다 !!!! –

0

URL 대신 ID로 호출하려고 했습니까? 또한 https/http 동일한 프로토콜이 여기에 적용됩니다. 호출하는 문서의 현재 프로토콜과 일치 시키려면 document.location.protocol과 함께 url을 호출해야 할 수도 있습니다.

<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> 
관련 문제