FB is not defined in Facebook Login

£可爱£侵袭症+ 提交于 2019-12-12 10:46:57

问题


I have the following code but the FB.login gives a FB is not defined javascript error.

What am I missing?

<html>
<body>
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function()
{
FB.init
({
    appId   :   'XXXX',
    status :    true,
    cookie :    true,
    xfbml   :   true
});
};

(function()
{
var e = document.createElement('script');
e.src = document.location.protocol +  '//connect.facebook.net/en_US/all.js';
e.async = true;
document.getElementById('fb-root').appendChild(e);
}());



FB.login(function(response)
{
if(response.session)
{
    if(response.perms)
    {
        alert('yippee');
    }
    else
    {
        alert('oh no');
    }
}
else
{
    alert('login silly');
}
}, {perms:'email,user_birthday'});


</script>
hello
<fb:login-button></fb:login>

</body>
</html>

回答1:


The function assigned to window.fbAsyncInit is run as soon as the SDK is loaded. Any code that you want to run after the SDK is loaded should be placed within this function and after the call to FB.init. For example, this is where you would test the logged in status of the user or subscribe to any Facebook events in which your application is interested.

Try by keeping other initialization code in between inialization and login function

Or keep FB.login code in $(document).ready

$(document).ready(function(){

FB.login(function(response)
{
if(response.session)
{
    if(response.perms)
    {
        alert('yippee');
    }
    else
    {
        alert('oh no');
    }
}
else
{
    alert('login silly');
}
}, {perms:'email,user_birthday'});

});



回答2:


You should not call to FB.login before JS-SDK is loaded, window.fbAsyncInit designed especially for this. If you move this call to be within window.fbAsyncInit function you'll be fine but be aware of fact that calling to FB.login opens popup, doing this without user interaction will be probably blocked by most browsers. If you want to use FB.login to handle login, you must do it on click or sumbit events...

BTW, you already have fb:login-button which is doing login once user clicks on it.




回答3:


You can also get this error if you try to launch the facebook example code on your machine (e.g. copying the code in a .html file and trying to open it in your browser). You will need to upload this to your server and run it from there.



来源:https://stackoverflow.com/questions/9092801/fb-is-not-defined-in-facebook-login

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!