FB 未定义 javascript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36956374/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 19:45:43  来源:igfitidea点击:

FB is not defined javascript

javascriptfacebookfacebook-graph-apifacebook-javascript-sdk

提问by Couim

I've a problem with FB JS SDK.

我有 FB JS SDK 的问题。

I'm trying to do a request to get the fan_count of a facebook page node.

我正在尝试请求获取 facebook 页面节点的 fan_count。

Here is my code from my html file into the body :

这是我从我的 html 文件到正文的代码:

<script>
  window.fbAsyncInit = function() {
      FB.init({
        appId      : 'your-app-id',
        xfbml      : true,
        version    : 'v2.5'
      });
    };

    (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/sdk.js";
      fjs.parentNode.insertBefore(js, fjs);
    }(document, 'script', 'facebook-jssdk'));
</script>

And when I'm using this on my js app, I use it :

当我在我的 js 应用程序上使用它时,我使用它:

init();

function init() {
  var id_fb  = "l214.animaux";
  while (true) {
    console.log("je suis ici");
    FB.api(
      '/' + id_fb +  '/',
      'GET',
      {"fields":"fan_count"},
      function(response) {
        alert(response.fan_count);
      }
    );
  }
}

But the error is that FB is not defined. Any suggestions ?

但错误是未定义FB。有什么建议 ?

回答by luschn

This would be correct, you need to use FB after the JS SDK is initialized. That being said, you definitely don′t want to call FB.api in an infinite loop, so i removed that part:

这是正确的,您需要在 JS SDK 初始化后使用 FB。话虽如此,你绝对不想在无限循环中调用 FB.api,所以我删除了那部分:

<script>
    function init() {
        FB.api(
          '/l214.animaux',
          {"fields":"fan_count"},
          function(response) {
            alert(response.fan_count);
          }
        );
    }

    window.fbAsyncInit = function() {
      FB.init({
        appId      : 'your-app-id',
        xfbml      : true,
        version    : 'v2.5'
      });

      init();
    };

    (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/sdk.js";
      fjs.parentNode.insertBefore(js, fjs);
    }(document, 'script', 'facebook-jssdk'));
</script>

Make sure you run this from an actual server, don′t just open your HTML files in a browser without at least a local server.

确保你从一个实际的服务器运行它,不要只在没有本地服务器的浏览器中打开你的 HTML 文件。

回答by Spark.Bao

I got this error because I write the init code in an independent js file, so, of course FBis not defined, because it should be window.FB.

我得到这个错误是因为我在一个独立的 js 文件中编写了 init 代码,所以,当然FB没有定义,因为它应该是window.FB.

my code:

我的代码:

class FacebookUtil {

  static init() {
    // comes from https://developers.facebook.com/docs/javascript/quickstart
    // notice FB should be window.FB
    window.fbAsyncInit = function() {
      window.FB.init({
        appId            : '...',
        autoLogAppEvents : true,
        xfbml            : true,
        version          : 'v2.10'
      });
      window.FB.AppEvents.logPageView();
    };

    (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/sdk.js";
      fjs.parentNode.insertBefore(js, fjs);
    }(document, 'script', 'facebook-jssdk'));
  }

  static login() {
    window.FB.login(...)
  }

}