Facebook Javascript api not loading

Facebook Javascript api not loading

本文关键字:loading not api Javascript Facebook      更新时间:2023-09-26

这是我的代码:

<!DOCTYPE html>
<html xmlns:fb="https://www.facebook.com/2008/fbml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <title>New JavaScript SDK & OAuth 2.0 based FBConnect Tutorial |     
Thinkdiff.net</title>
</head>
<body>
<div id="fb-root"></div>
<script>
  window.fbAsyncInit = function() {
  FB.init({
  appId      : '154062364721089', // App ID
  channelUrl : '//WWW.YOUR_DOMAIN.COM/channel.html', // Channel File
  status     : true, // check login status
  cookie     : true, // enable cookies to allow the server to access the session
  xfbml      : true,  // parse XFBML
  oauth: true
  });
  // Additional initialization code here
   alert('Api loaded');
   FB.login(function(response)
   {
    console.log(response);
   });
   };
  // Load the SDK Asynchronously
 (function(d){
 var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
 if (d.getElementById(id)) {return;}
 js = d.createElement('script'); js.id = id; js.async = true;
 js.src = "connect.facebook.net/en_US/all.js";
 ref.parentNode.insertBefore(js, ref);
 }(document));
</script>
test
</body>
</html>

但是当我在浏览器中打开此文件时,Facebook api 无法加载。我只看到屏幕上印有"测试"。我相信浏览器应该联系 facebook 以加载 API - 我认为这将被称为浏览器底部的连接消息,这不会发生。

解析 JavaScript 中的错误应该是首先要解决的问题。您的代码在 FB.init() 调用(xfbml 和 oauth)中的最后两个字典元素之间缺少逗号

编辑 您拥有的代码在 IE 和 Chrome 中对我来说效果很好,除非您从本地计算机运行它(即双击光盘上的 HTML 文件),因为加载 API 时 URL 中的"//"

听起来您希望Facebook自动提示用户连接/允许/登录该应用程序。为此,您需要在 fb api 初始化后添加以下内容(代码在 // Additional initialization code here 之后)...

// Additional initialization code here
alert('Api loaded');
FB.login(function(response)
{
    console.log(response);
});

编辑:另外,尝试删除<script>引用并使用Facebook文档中的异步代码 - https://developers.facebook.com/docs/reference/javascript/

  (function(d){
     var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
     if (d.getElementById(id)) {return;}
     js = d.createElement('script'); js.id = id; js.async = true;
     js.src = "//connect.facebook.net/en_US/all.js";
     ref.parentNode.insertBefore(js, ref);
   }(document));

不确定这是否完全相关,找到了这个线程,因为我在以下行之后得到了"js 未定义"js = d.createElement('script'); js.id = id; js.async = true;

事实证明,解决方案是以下行:if (d.getElementById(id)) {return;}后面需要一个尾随;,而不是在行括号内:

if (d.getElementById(id)) {return;};

if (d.getElementById(id)) {return};

工作,而

if (d.getElementById(id)) {return;}

抛出错误。.