FB未定义为javascript

FB is not defined javascript

本文关键字:javascript 未定义 FB      更新时间:2023-09-26

我在使用FB JS SDK时遇到问题。

我正在尝试做一个请求,以获取facebook页面节点的粉丝数。

这是我从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>

当我在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);
      }
    );
  }
}

但错误在于FB没有定义。有什么建议吗?

这是正确的,您需要在初始化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>

确保你在实际的服务器上运行,不要只在没有本地服务器的情况下在浏览器中打开HTML文件。

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

我的代码:

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(...)
  }
}

如果您正在处理的是react,这里是我的解决方案代码。你必须停用脚本阻止设置(在勇敢的浏览器中,我看到它被阻止了,这就是问题的原因)

import React from "react";
export const FacebookButton = ()=>{
    const handleClick=()=>{
        FB.login(function(response) {
            if (response.authResponse) {
            //  console.log('Welcome!  Fetching your information.... ');
             FB.api('/me', function(response) {
              //  console.log('Good to see you, ' + response.name + '.');
             });
            } else {
            //  console.log('User cancelled login or did not fully authorize.');
            }
        });
    }
    React.useEffect(()=>{
        window.fbAsyncInit = function() {
            window.FB.init({
                appId      : '380427166785990',
                xfbml      : true,
                version    : 'v11.0'
              });
            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'));
        
    },[])
    
    return(
        <button  onClick={handleClick}>Login</button>    )
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
import React from "react";
export const FacebookButton = ()=>{
    const handleClick=()=>{
        FB.login(function(response) {
            if (response.authResponse) {
            //  console.log('Welcome!  Fetching your information.... ');
             FB.api('/me', function(response) {
              //  console.log('Good to see you, ' + response.name + '.');
             });
            } else {
            //  console.log('User cancelled login or did not fully authorize.');
            }
        });
    }
    React.useEffect(()=>{
        window.fbAsyncInit = function() {
            window.FB.init({
                appId      : '0500550055050',
                xfbml      : true,
                version    : 'v11.0'
              });
            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'));
        
    },[])
    
    return(
        <button  onClick={handleClick}>Login</button>    )
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>