3

我收到常见的 JS 错误“未捕获的引用错误:未定义 FB”

关于同一个问题有很多问题,我已经尝试了 stackoverflow 上可用的每个解决方案,但无济于事。显然,人们有时似乎会遇到这个问题,我一直都在遇到这个问题。谁能发现错误?我暂时没有使用 chnnel url 文件

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<div id="fb-root"></div>
<script type="text/javascript">
  window.fbAsyncInit = function() 
 {
    // init the FB JS SDK
    FB.init=({
      appId      : 'xxxxxxxxxxx', // App ID from the App Dashboard
      //channelUrl : 'www.something.in/channel.html', // Channel File for x-domain communication
      status     : false, // check the login status upon init?
      cookie     : true, // set sessions cookies to allow your server to access the session?
      xfbml      : true  // parse XFBML tags on this page?
      }) ;

    // Additional initialization code such as adding Event Listeners goes here
 } ;



</script>

<script type="text/javascript">
  // Load the SDK's source Asynchronously
  // Note that the debug version is being actively developed and might 
  // contain some type checks that are overly strict. 
  // Please report such bugs using the bugs tool.
   (function(d, debug){ 
   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" + (debug ? "/debug" : "") + ".js";
     ref.parentNode.insertBefore(js, ref);
   }(document, /*debug*/ false));



FB.getLoginStatus(function(response) {
  if (response.status === 'connected') {
    // the user is logged in and has authenticated your
    // app, and response.authResponse supplies
    // the user's ID, a valid access token, a signed
    // request, and the time the access token 
    // and signed request each expire
    var uid = response.authResponse.userID;
    var accessToken = response.authResponse.accessToken;
    alert(uid) ;
  } else if (response.status === 'not_authorized') {
    // the user is logged in to Facebook, 
    // but has not authenticated your app
    alert("not authorised");
  } else {
    // the user isn't logged in to Facebook.
    alert("NOTHING") ;
  }
 });

</script>

</body>

</html>
4

3 回答 3

0

你能让基本的例子工作吗? https://developers.facebook.com/docs/facebook-login/getting-started-web/ (第3节下的全文)

当我包含我自己的代码库 FB 从未定义时,我可以获得加载的基本简单示例。事实证明,我已经将“Object”原型化为有一个名为“copyProperties”的方法。我将该方法更改为“copyProps”,现在一切正常。因此,如果 Facebook 出于自己的目的对 Object 进行原型设计,那么我猜还有其他一些潜在的冲突。

当然,这一定是每个人都说不要原型 Object 的原因之一,但它很方便!明明是不可数的,所以不要对我大吼大叫。

于 2013-08-02T21:43:42.790 回答
0

我有同样的问题,我尝试了一切。我只是通过 jQuery 请求 Facebook 脚本然后对其进行初始化来解决它:

$(document).ready(function()
{
    $.getScript("http://connect.facebook.net/en_US/all.js#xfbml=1", function () {
        FB.init({ appId: 'xxxxxxxx', status: false, cookie: true, xfbml: true });
    });
});

FB.getLoginStatus(function(response) {
    if (response.status === 'connected') {
        // the user is logged in and has authenticated your
        // app, and response.authResponse supplies
        // the user's ID, a valid access token, a signed
        // request, and the time the access token 
        // and signed request each expire
        var uid = response.authResponse.userID;
        var accessToken = response.authResponse.accessToken;
        alert(uid) ;
    } else if (response.status === 'not_authorized') {
        // the user is logged in to Facebook, 
        // but has not authenticated your app
        alert("not authorised");
    } else {
        // the user isn't logged in to Facebook.
        alert("NOTHING") ;
    }
});
于 2017-02-21T18:58:58.837 回答
-1

您的代码在 FB.init=({.

您必须在 FB.init 之后删除等号,您的代码如下所示:

FB.init({
  appId      : 'xxxxxxxxxxx', // App ID from the App Dashboard
  //channelUrl : 'www.something.in/channel.html', // Channel File for x-domain communication
  status     : false, // check the login status upon init?
  cookie     : true, // set sessions cookies to allow your server to access the session?
  xfbml      : true  // parse XFBML tags on this page?
  }) ;

你又犯了一个错误。你的 div id 是 fb-root 但在初始化时你声明了 facebook-jssdk id

只要确保两者应该相同

于 2013-01-02T05:15:18.927 回答