fbgetloginstatus: FB未定义。

3

我遇到了常见的JS错误“Uncaught Reference Error: FB未定义”

关于同样的问题有很多问题,我已经尝试了stackoverflow上提供的每个解决方案,但都没有成功。显然,有时人们会遇到这个问题,而我一直在遇到它。有谁能发现错误吗?目前我不使用频道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>

可能是重复的问题: http://facebook.stackoverflow.com/questions/5331165/fb-is-not-defined-problem - Sahil Mittal
嗯,Sahil,我尝试了那里发布的所有解决方案。它们都不起作用。一个区别是上面链接中的用户有时会遇到错误,但是我的脚本至今还没有起作用。 - varun Kishnani
但我猜你没有正确地遵循它们。这些调用是异步的,你只能在FB.init()之后调用额外的代码。在FB.Init()之后调用你的FB.getLoginStatus函数(其中写有// additional initialization code..)。 - Sahil Mittal
3个回答

0

您能让基本示例正常运行吗? https://developers.facebook.com/docs/facebook-login/getting-started-web/ (在第3节下的完整文本)

我可以加载基本简单示例,但是当我包含自己的代码库时,FB从未被定义。 事实证明,我已经为“Object”原型编写了一个名为'copyProperties'的方法。 我将该方法更改为'copyProps',现在一切正常。因此,如果Facebook为其自己的目的而原型化Object,那么我想还有其他潜在的冲突。

当然,这可能是人们说不要进行对象原型设计的原因之一,但非常方便!显然,它不可枚举,所以不要过于责备我。


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") ;
    }
});

-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。

确保两者相同即可。


我已经进行了更改,但这也没有帮助 :( FB未定义仍然存在。 - varun Kishnani

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接