javascript Facebook 登录按钮错误:在调用 FB.init() 之前调用了 FB.login()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4909638/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
FaceBook Login Button Error: FB.login() called before calling FB.init()
提问by Genadinik
I am getting this error after doing this code:
执行此代码后,我收到此错误:
<div id="fb-root"></div>
<script src="http://connect.facebook.net/en_US/all.js"></script>
<script>
FB.init({
appId : 'realIdInTheCode',
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
</script>
<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#appId=realIdInTheCode&xfbml=1">
</script><fb:login-button show-faces="false" width="200" max-rows="1"></fb:login-button>
Which to my thinking is calling the init() before the login()
我的想法是在 login() 之前调用 init()
But I still get that error: Error: FB.login() called before calling FB.init().
但我仍然收到那个错误:错误:在调用 FB.init() 之前调用了 FB.login()。
I am in kind of total confusion at this point, so I'd really appreciate the help :)
在这一点上我完全困惑,所以我真的很感激你的帮助:)
采纳答案by Genadinik
For me, it turned out to be some problem with the app itself. So I deleted the old app in FB and made a new one, and things just started to work.
对我来说,结果证明是应用程序本身存在一些问题。所以我删除了 FB 中的旧应用程序并创建了一个新应用程序,事情才刚刚开始。
回答by Nishant
I used to think FBML was deprecated. I've just tested, this works for me
我曾经认为 FBML 已被弃用。我刚刚测试过,这对我有用
<html>
<head>
</head>
<body>
<div id="fb-root"></div>
<script>
//initializing API
window.fbAsyncInit = function() {
FB.init({appId: 'MYAPPID', status: true, cookie: true,
xfbml: true});
};
(function() {
var e = document.createElement('script'); e.async = true;
e.src = document.location.protocol +
'//connect.facebook.net/en_US/all.js';
document.getElementById('fb-root').appendChild(e);
}());
</script>
<button type="button" onclick="fblogin();">Facebook Login</button>
<script>
//your fb login function
function fblogin() {
FB.login(function(response) {
//User logged in!
if (response.session) {
alert('YAY!');
} else {
// user cancelled login
alert('NAY!');
}
}, {perms:'email,read_stream,publish_stream'});
}
</script>
</body>
</html>
OR
或者
<html>
<head>
</head>
<body>
<script src="http://connect.facebook.net/en_US/all.js"></script>
<script>
FB.init({
appId : 'MYAPPID',
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
</script>
<button type="button" onclick="fblogin();">Facebook Login</button>
<script>
//your fb login function
function fblogin() {
FB.login(function(response) {
//User logged in!
if (response.session) {
alert('YAY!');
} else {
// user cancelled login
alert('NAY!');
}
}, {perms:'email,read_stream,publish_stream'});
}
</script>
</body>
</html>
refer:
参考:
回答by Pavel Surmenok
Try this code:
试试这个代码:
<div id="fb-root"></div>
<script src="http://connect.facebook.net/en_US/all.js"></script>
<script>
FB.init({
appId : 'realIdInTheCode',
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
</script>
<fb:login-button show-faces="false" width="200" max-rows="1"></fb:login-button>

