【问题标题】:Facebook LIKE-button using jquery使用 jquery 的 Facebook LIKE 按钮
【发布时间】:2013-11-15 03:34:00
【问题描述】:

我有这个简单的 Facebook 应用程序。它使用 jQuery 并根据 Facebook 自己的规范和示例代码制作。该应用仅供喜欢该页面的访问者使用。

我已经成功地用 PHP 构建了它,但由于其他原因,我需要用 Javascript 来构建它。但是,“has-user-liked-page”功能没有触发,我尝试了十几种这样的解决方案:event is not fired after clicking facebook like button?

现在,Facebook 文档建议这样做:https://developers.facebook.com/docs/reference/javascript/FB.Event.subscribe

我的代码中可能最相关的部分如下所示:

....
<body>
<div id="fb-root"><iframe id="contentIframe" scrolling="no"></iframe></div>
    <script language="javascript">
        $(document).ready(function() {
            $.ajaxSetup({ cache: true });
                $.getScript('//connect.facebook.net/en_UK/all.js', function(){
                    FB.init({
                        appId: '247171102105...',
                        channelUrl: 'https://facebook.****.com/customer1/test/channel.html',
                    }); 

                    FB.Event.subscribe('edge.create', function(href, widget) {
                        alert('You liked the URL: ' + href);
                    });

                    FB.getLoginStatus(updateStatusCallback);
                });
            });

            function updateStatusCallback() {           
                FB.login(function(response) {
                    if (response.authResponse) {
                        FB.api('/me', function(response) {
                            document.getElementById("contentIframe").src = "kalender.php?userID="+response.id+"&userName="+response.name+"&email="+response.email;
                            //alert(JSON.stringify(response, null, 4));
                        });
                    } else {
                        document.getElementById("contentIframe").src = "fallback.php";
                    }
                }, {scope:'email,publish_stream,user_photos'});
                FB.Canvas.setAutoGrow();
            }
    </script>
    ....

你有什么线索,为什么警报命令从未被触发?

【问题讨论】:

    标签: javascript facebook facebook-graph-api


    【解决方案1】:

    经过一个漫长的夜晚,我找到了解决办法。我结合了 PHP 和 jQuery API,以便 PHP 部分检查类似。 jQuery 处理所有其他事情 - 权限、转发等。它不是很优雅,但我没有找到其他方法。

    这在文件的顶部:

    $facebook = new Facebook(array(
            'appId'  => '247171102105***',
            'secret' => '************************',
        ));
        $signed_request = $facebook->getSignedRequest();
        $like_status = $signed_request["page"]["liked"];
        if ($like_status == 1) {
            $url = "kalender";
        } else {
            $url = "userDoesntLike";
        }
    

    在 Javascript 部分:

    function updateStatusCallback() {           
        FB.login(function(response) {
            if (response.authResponse) {
            FB.api('/me', function(response) {
                    document.getElementById("contentIframe").src = "<?=$url;?>.php?userID="+response.id+"&userName="+response.name+"&email="+response.email;
                    //alert(JSON.stringify(response, null, 4));
    });
                } else {
                    document.getElementById("contentIframe").src = "fallback.php";
                }
            }, {scope:'email,publish_stream,user_photos'});
            FB.Canvas.setAutoGrow();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-23
      • 1970-01-01
      • 1970-01-01
      • 2011-12-06
      • 1970-01-01
      • 2011-07-03
      • 1970-01-01
      • 2011-09-03
      相关资源
      最近更新 更多