成功登录后,Facebook Connect不会处理jQuery ajax调用

时间:2011-09-03 16:58:54

标签: facebook facebook-graph-api

我有以下脚本:

    <script>
        window.fbAsyncInit = function () {
            FB.init({ appId: '<%: Facebook.FacebookApplication.Current.AppId %>',
                status: true,
                cookie: true,
                xfbml: true,
                oauth: true
            });

            function FBLogin() {
                FB.login(function (response) {
                    if (response.authResponse) {                            
                        ConnectFacebookUser(response.authResponse.accessToken);
                    } else {
                        alert('User cancelled login or did not fully authorize.');
                    }
                }, { scope: 'email' });
            }

            $('#fb-button').live('click', function (e) {
                FBLogin();

                e.preventDefault();
            });

            function ConnectFacebookUser(at) {
                var postdata = "at=" + at;
                alert('This is the access token : ' + at);

                $.ajax({
                    type: "POST",
                    dataType: "json", // what data type to expect from the server
                    url: "/FBConnect.ashx",
                    data: postdata,
                    success: function (data) {
                        // do nothing
                        alert('all good');
                    },
                    error: function (xhr, status, error) {
                        alert('error occured in FBConnect.ashx');
                    }
                });

                alert('finished');
            }
        };

        (function () {
            var e = document.createElement('script'); e.async = true;
            e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
            document.getElementById('fb-root').appendChild(e);
        } ());

    </script>

对于html片段:

    <a href="#" id="fb-button">Login with facebook</a>
    <div id="fb-root"></div>

当点击链接时,会显示一个Facebook的弹出窗口,当我输入我的登录详细信息时,我只会看到一条警告说“这是访问令牌......”但其余代码没有得到处理。如果我再次点击“登录facebook”链接,那么一切都会正常处理。

可能是什么问题?

2 个答案:

答案 0 :(得分:1)

查看https://github.com/xocialhost/jquery.xocialCore.js/blob/master/jquery.xocialCore.js以查看jQuery特定的方法。我注意到的一件事是,调用FB.login时的jQuery单击函数在我测试的几个浏览器中触发了弹出窗口阻止程序。我将其设置为解决该问题,但它也是一种添加在init序列期间正常运行的回调的方法。

答案 1 :(得分:1)

它对我有用。我收到消息'这是访问代码',然后我收到'完成'警报。检查您的Chrome调试工具,查找可能以静默方式发生的任何脚本错误。