1

我有设置

<div id="fb-root"></div><div class="fb-login-button" data-show-faces="true" data-width="400" data-max-rows="1" scope="email, publish_stream"></div><script type="text/javascript">
window.fbAsyncInit = function () {
    FB.init({
        appId: 'xxx', // App ID
        status: true, // check login status
        cookie: true, // enable cookies to allow the server to access the session
        xfbml: true,  // parse XFBML,
        oauth : true
    });

    FB.Event.subscribe('auth.authResponseChange', function (response) {
        if (response.status === 'connected') {
            // the user is logged in and has authenticated your
            // app, and response.authResponse supplies
            // the user's ID, a valid access token, a signed
            // request, and the time the access token 
            // and signed request each expire
            var uid = response.authResponse.userID;
            var accessToken = response.authResponse.accessToken;

            // TODO: Handle the access token

            $.post('/facebook/login', { accessToken: accessToken }, function (response) { if(response) {alert('Logged in')} });

        } else if (response.status === 'not_authorized') {
            // the user is logged in to Facebook, 
            // but has not authenticated your app
        } else {
            // the user isn't logged in to Facebook.
        }
    });


};

// Load the SDK Asynchronously
(function (d) {
    var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
    if (d.getElementById(id)) { return; }
    js = d.createElement('script'); js.id = id; js.async = true;
    js.src = "//connect.facebook.net/en_US/all.js";
    ref.parentNode.insertBefore(js, ref);
} (document));

问题是当用户单击登录按钮并接受我的应用程序时,登录对话框关闭并且没有触发任何事件,因此我的帖子 $.post('/facebook/login') 也永远不会触发,除非我按 F5。

如何在登录对话框关闭后立即触发事件?

4

2 回答 2

0

只是分享。在阅读 Facebook 文档以获取答案时遇到可能的解决方案。

http://facebook.stackoverflow.com/questions/3548493/how-to-detect-when-facebooks-fb-init-is-complete

你可以在 fbEnsureInit(); 中运行你的 $.post('/facebook/login'); <-- 指的是 serg 的回答。

使用间隔回调来检查登录状态可能是一种方法。

于 2012-05-13T03:51:18.650 回答
-1

您可以使用以下命令重新加载页面:window.location.reload();因此您无需按 F5,

一个 FB.login 示例:

FB.login(function(response) {
    if (response.authResponse) {
        console.log('Application authorized.');
        window.location.reload();
    } else {
        console.log('You cancelled login or you did not fully authorize the app.');
    }
}, { scope: 'email' });

或者您可以使用订阅事件:

FB.Event.subscribe('auth.authResponseChange', function(response) {
    console.log('The status of the session changed to: '+response.status);
    window.location.reload();
});
于 2012-05-13T06:33:07.340 回答