3

我需要知道用户是否喜欢我的 Facebook 页面(不是在 fb 应用程序中,而是在外部网站上)。我试图使用这个片段:http: //jsfiddle.net/alexgarry/qdfL6/5/

我将它复制到文档后它没有工作。我的脚本加载后,会出现一个带有错误的新弹出窗口:“再试一次”。

我的代码:

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js"></script>
<style>
#container_notlike, #container_like {
    display:none
}
</style>
<script src="http://connect.facebook.net/en_US/all.js"></script>
<script>
  FB.init({
    appId  : 'MY_APP_ID',
    status : true,
    cookie : true,
    xfbml  : true  
  });

$(document).ready(function(){
    FB.login(function(response) {
        if (response.status == 'connected') {
            var user_id = response.authResponse.userID;
            var page_id = "230305033727799"; // coca cola page https://www.facebook.com/cocacola
            var fql_query = "SELECT uid FROM page_fan WHERE page_id="+page_id+" and uid="+user_id;

            FB.api({
                method: 'fql.query',
                query: fql_query
            },
            function(response){
                if (response[0]) {
                    $("#container_like").show();
                } else {
                    $("#container_notlike").show();
                }
            }
            );    
        } else {
        // user is not logged in
        }
    });
});
</script>
</head>
<body>
<div id="fb-root"></div>
<div id="container_notlike">
YOU DONT LIKE
</div>
<div id="container_like">
YOU LIKE
</div>
</body>
</html>

有任何想法吗?

问候。

4

1 回答 1

3
<!DOCTYPE html>
<html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <style type="text/css">
        #container_notlike, #container_like {
            display:none
        }​
    </style>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function() {
            FB.login(function(response) {
                if (response.status == 'connected') {
                    var user_id = response.authResponse.userID;
                    var page_id = "40796308305";  

                    FB.api('/me/likes/'+page_id, function(response) {
                        if (response.data[0]) {
                            $("#container_like").show();
                        } else {
                            $("#container_notlike").show();
                        }
                    });
                } else {
                    // user is not logged in                        
                }
            });
        });
    </script>
</head>
<body>
    <div id="fb-root"></div>
    <script src="https://connect.facebook.net/en_US/all.js"></script>
    <script>
        FB.init({appId: 'MY_APP_ID', status: true, cookie: true, xfbml: true});
    </script>
    <div>
        <div id="container_notlike">
            YOU DONT LIKE
        </div>
        <div id="container_like">
            YOU LIKE
        </div>​
    </div>
</body>

将 MY_APP_ID 替换为您的 APP_ID。

于 2012-06-15T12:35:38.717 回答