2013-01-01 15 views
3

我越來越常見的JS錯誤「未捕獲參考錯誤:FB沒有定義」fbgetloginstatus:FB沒有定義

有很多對同一問題的問題,我已經嘗試了 計算器可用的每個解決方案但無濟於事。顯然,人們似乎有時會遇到這個問題,我一直在得到它。任何人都可以發現錯誤?我沒有使用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> 
+0

可能重複:我只是通過請求由jQuery的Facebook的腳本,然後初始化它解決它http://facebook.stackoverflow.com/questions/5331165/fb-is-not-defined-problem –

+0

好Sahil,我嘗試了所有在那裏發佈的解決方案。他們不工作。一個區別是,上面的鏈接中的用戶有時會得到錯誤,但我的腳本如上所述還沒有工作。 –

+0

但我想你沒有正確地跟着他們。這些調用是異步的,你只能在FB.init()之後調用額外的代碼。在FB.Init()之後調用FB.getLoginStatus函數(其中//附加初始化代碼..寫入)。 –

回答

-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-根,但在初始化時聲明Facebook的jssdk ID

只要確保兩者都應該是相同的

+0

我做了改變,這也沒有幫助:(FB沒有定義的優點 –

0

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

我能得到基本的簡單的例子來加載,但是當我包括我自己的代碼庫從未被定義FB。 事實證明,我已經將「Object」原型化爲一個名爲「copyProperties」的方法。 我將該方法更改爲'copyProps',現在它全部正常工作。所以,如果Facebook爲了他們自己的目的而對Object進行原型設計,那麼我猜想還有其他潛在的衝突。

當然,這一定是所有人都說不要原型對象的原因之一,但這麼方便!很明顯,它不是可以枚舉的,所以不要太難過。

0

我有同樣的問題,我嘗試了一切。

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