0

我正在尝试进行以下简单的 facebook 集成:

我只想读取之前授权的应用程序状态,连接或未授权我想处理结果。

但我得到了错误FB.getLoginStatus() called before calling FB.init().init假设我打电话时没有完成,这是有道理的getLoginStatus

如何在不添加异步动态 js 引用的情况下解决这个问题?

<script language="javascript">
    $(document).ready(function () {

        window.fbAsyncInit = function () {
            FB.init({
                appId: 'myappid', // App ID
                status: true, // check login status
                cookie: true, // enable cookies to allow the server to access the session
                xfbml: true  // parse XFBML
            });
        };


        FB.getLoginStatus(function (response) {
            alert(response.status);
            if (response.status == "connected") {
                $("#lkLogin").css("display", "none");
                $("#spUser").css("display", "inline");

                $("#spUser").html(response.name);

            } else if (response.status == "not_authorized") {
                $("#lkLogin").css("display", "inline");
                $("#spUser").css("display", "none");
            }
        });

    });
</script>

<a href="#" id="lkLogin" style="display:inline;">Facebook Login</a>
<span id="spUser" style="display:none;"></span>
4

2 回答 2

0

您应该在调用FB.getLoginStatus()后立即调用该函数FB.init();因为init()被称为异步。所以发生的事情是,在调用FB之前没有初始化;getLoginstatus()因此你应该这样称呼它同步 -

window.fbAsyncInit = function () {
    FB.init({
         appId: 'myappid', // App ID
         status: true, // check login status
         cookie: true, // enable cookies to allow the server to access the session
         xfbml: true  // parse XFBML
     });

     // call FB.getloginstatus function here
};
于 2013-07-26T17:20:04.333 回答
-2

这应该这样做:

<script language="javascript">


            window.fbAsyncInit = function () {
                FB.init({
                    appId: 'myappid', // App ID
                    status: true, // check login status
                    cookie: true, // enable cookies to allow the server to access the session
                    xfbml: true  // parse XFBML
                });
            };

    $(document).ready(function () {
            FB.getLoginStatus(function (response) {
                alert(response.status);
                if (response.status == "connected") {
                    $("#lkLogin").css("display", "none");
                    $("#spUser").css("display", "inline");

                    $("#spUser").html(response.name);

                } else if (response.status == "not_authorized") {
                    $("#lkLogin").css("display", "inline");
                    $("#spUser").css("display", "none");
                }
            });

        });
    </script>

    <a href="#" id="lkLogin" style="display:inline;">Facebook Login</a>
    <span id="spUser" style="display:none;"></span>
于 2013-07-26T15:08:58.347 回答