Facebook Javascript SDK FB未定义

时间:2014-04-26 05:20:41

标签: javascript facebook facebook-graph-api facebook-javascript-sdk

使用Facebook Javascript SDK时出现错误FB not defined

FB.init({
    appId: '{APP ID}',
    status: true,
    cookie: true,
    xfbml: true
});

Jsfiddle

1 个答案:

答案 0 :(得分:6)

两件事 -

  1. 我无法看到JS SDK已加载。

  2. 您必须在异步加载SDK后调用FB函数,否则会抛出FB is not defined

  3. 所以,要解决它 -

    // This will be triggered automatically after the SDK is loaded successfully
    // write your FB fucntions inside this
    window.fbAsyncInit = function() {
       FB.init({
         appId: '{app-id}',
         status: true,
         cookie: true,
         xfbml: true
       });
    
       FB.getLoginStatus(function (response) {
         if (response.status === 'connected') {
            GetData();
         } else {
            Login();
         }
      });
    };
    
    // JS SDK - this will be loaded asynchronously
    (function(d, s, id){
      var js, fjs = d.getElementsByTagName(s)[0];
      if (d.getElementById(id)) {return;}
      js = d.createElement(s); js.id = id;
      js.src = "//connect.facebook.net/en_US/all.js";
      fjs.parentNode.insertBefore(js, fjs);
    }(document, 'script', 'facebook-jssdk'));
    
    function Login() {
       ...
       ...
    }
    
    function GetData() {
       ...
       ...
    }
    

    Reference

    修改

    我看到你要求获得许可email但尚未使用它。使用实际的电子邮件(而不是facebook)与 - response.email