未捕获的ReferenceError:使用FB.getLoginStatus时未定义FB

Xee*_*een 30 javascript facebook facebook-login

我正在尝试检查用户是否已使用Facebook登录并在JS控制台中获取该错误.我的代码看起来像这样:

<div id="fb-root"></div>
<script>
    window.fbAsyncInit = function () {
        FB.init({
            appId: '######', // App ID
            status: true, // check login status
            cookie: true, // enable cookies to allow the server to access the session
            xfbml: true  // parse XFBML
        });
    };

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

    FB.getLoginStatus(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;
        } 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.
        }
    });
</script>
Run Code Online (Sandbox Code Playgroud)

可能是什么问题,是否有任何关于如何解决它的sugestions?

jai*_*bow 52

在Chrome中,您将收到错误:未捕获的ReferenceError:未定义FB.

我喜欢在Facebook的初始化函数fbAsyncInit()中触发自定义事件.然后,我不仅限于在fbAsyncInit函数中执行所有与FB相关的脚本.我可以把它放在任何地方,只需要监听要触发的自定义事件.

window.fbAsyncInit = function() {
    FB.init({
        appId      : '123456789',
        status     : true,
        cookie     : true,
        xfbml      : true  
    });

    $(document).trigger('fbload');  //  <---- THIS RIGHT HERE TRIGGERS A CUSTOM EVENT CALLED 'fbload'
};

//MEANWHILE IN $(document).ready()
$(document).on(
    'fbload',  //  <---- HERE'S OUR CUSTOM EVENT BEING LISTENED FOR
    function(){
        //some code that requires the FB object
        //such as...
        FB.getLoginStatus(function(res){
            if( res.status == "connected" ){
                FB.api('/me', function(fbUser) {
                    console.log("Open the pod bay doors, " + fbUser.name + ".");
                });
            }
        });

    }
);
Run Code Online (Sandbox Code Playgroud)


CBr*_*roe 45

在加载和/或初始化SDK之前,您正在调用FB.getLoginStatus.要等待,那就是fbAsyncInit事件的用途.所以把方法调用放在那里.

  • @ user1735921:您需要确保在初始化SDK之前不要调用任何FB方法.因此,对于您想要"自动"调用的方法(没有用户交互,例如单击),您应该在`FB.init`调用之后将它们放入`window.fbAsyncInit`函数中. (2认同)

bas*_*ero 10

我使用以下简单的方法,它可以正常工作:

在head我加载SDK 的部分中:

<script type="text/javascript" src="//connect.facebook.net/en_US/sdk.js"></script>
Run Code Online (Sandbox Code Playgroud)

然后在body您的实际内容页面中,我使用了以下内容:

<script>

  function statusChangeCallback(response) {
    console.log('statusChangeCallback');
    console.log(response);
    if (response.status === 'connected') {
      testAPI();

    } else if (response.status === 'not_authorized') {
      FB.login(function(response) {
        statusChangeCallback2(response);
      }, {scope: 'public_profile,email'});

    } else {
      alert("not connected, not logged into facebook, we don't know");
    }
  }

  function statusChangeCallback2(response) {
    console.log('statusChangeCallback2');
    console.log(response);
    if (response.status === 'connected') {
      testAPI();

    } else if (response.status === 'not_authorized') {
      console.log('still not authorized!');

    } else {
      alert("not connected, not logged into facebook, we don't know");
    }
  }

  function checkLoginState() {
    FB.getLoginStatus(function(response) {
      statusChangeCallback(response);
    });
  }

  function testAPI() {
    console.log('Welcome!  Fetching your information.... ');
    FB.api('/me', function(response) {
      console.log('Successful login for: ' + response.name);
      document.getElementById('status').innerHTML =
        'Thanks for logging in, ' + response.name + '!';
    });
  }

  $(document).ready(function() {
    FB.init({
      appId      : '1119999988888898981989819891',
      xfbml      : true,
      version    : 'v2.2'
    });
    checkLoginState();
  });
</script>
Run Code Online (Sandbox Code Playgroud)