2

这是我的代码:

<!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 - 我认为这将被称为浏览器底部的连接消息,这不会发生。

4

3 回答 3

2

Parse errors in your JavaScript should be the first things to address. Your code is missing a comma between the last two dictionary elements within the FB.init() call (xfbml and oauth)

Edit The code as you have it works fine for me in IE and Chrome, except when you run it from your local machine (i.e. double click an HTML file on your disc) due to the '//' in the URL when loading the API

于 2012-04-30T19:38:54.033 回答
1

听起来您希望 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));
于 2012-04-30T19:15:18.673 回答
1

不确定这是否完全相关,找到了这个线程,因为我在该行之后得到了“js is not defined”: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;}

抛出错误..

于 2014-01-19T01:15:08.220 回答