本文介绍了FB没有定义javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有FB JS SDK的问题。

I've a problem with FB JS SDK.

我正在尝试一个请求来获取一个Facebook页面节点的fan_count。

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

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

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

<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>

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

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

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);
      }
    );
  }
}

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

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

推荐答案

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

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:

<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>

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

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.

这篇关于FB没有定义javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 18:40