FB 未定义 javascript [英] FB is not defined javascript

查看:34
本文介绍了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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆