繁体   English   中英

如何修复“未捕获的ReferenceError:未定义FB”?

[英]How do I fix “Uncaught ReferenceError: FB is not defined”?

在Google Chrome浏览器中运行此代码时,出现以下错误:

未捕获的ReferenceError :未定义FB

我已经阅读了关于同一错误的其他多个问题,但似乎没有一个解决方案。

window.fbAsyncInit = function() {
  FB.init({
    appId : 'xxxxxxxxxxxxxxx',
    autoLogAppEvents : true,
    xfbml : true,
    version : 'v3.2'
  });
};

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

 FB.api(
   '/me',
   'GET',
   {"fields":"id,name,posts{picture,message,comments{likes}}"},
   function(response) {
       // Insert your code here
   }
 );

我希望Facebook API返回我选择的字段。

我的代码是Facebook文档中显示的代码的副本。

在初始化JS SDK之前不能使用FB:

window.fbAsyncInit = function() {
  FB.init({
    appId : 'xxxxxxxxxxxxxxx',
    autoLogAppEvents : true,
    xfbml : true,
    version : 'v3.2'
  });

  FB.api(
   '/me',
   'GET',
   {"fields":"id,name,posts{picture,message,comments{likes}}"},
   function(response) {
       console.log(response);
   }
  );
};

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

顺便说一句,未经用户授权,此代码将无法使用。 此处详细说明了如何执行此操作: https//developers.facebook.com/docs/facebook-login/web/

docs中还有一个完整的示例: https : //developers.facebook.com/docs/facebook-login/web/#example

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM