Javascript 在控制台中调用 FB.init() 错误之前调用 FB.getLoginStatus()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8048564/
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
FB.getLoginStatus() called before calling FB.init() error in console
提问by mauzilla
I am trying to check if a user is logged in with my app, but I am getting a
我正在尝试检查用户是否使用我的应用程序登录,但我收到了
FB.getLoginStatus() called before calling FB.init().
在调用 FB.init() 之前调用 FB.getLoginStatus()。
error in the console. The setSize
appears to work (although not entirely 1,710 height but definitely around 1,500) so I cannot understand why the getLoginStatus()
gives the error.
控制台中的错误。在setSize
出现工作(虽然不是全部1710的高度,但肯定1500左右),所以我不明白为什么getLoginStatus()
给错误。
I also double checked with the appID (removed below) and that is definitely correct. The script is included via <script src="file.js" type="text/javascript"></script>
right below my <body>
and <div id="fb-root"></div>
div.
我还仔细检查了 appID(在下面删除),这绝对是正确的。该脚本包含<script src="file.js" type="text/javascript"></script>
在 my<body>
和<div id="fb-root"></div>
div正下方。
window.fbAsyncInit = function() {
FB.init({
appId : 'APPID', // App ID
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
oauth : true, // enable OAuth 2.0
xfbml : true // parse XFBML
});
FB.Canvas.setSize({width: 520, height: 1710});
FB.Canvas.setAutoResize(100);
FB.getLoginStatus(function(response) {
if (response.authResponse) {
// logged in and connected user, someone you know
alert("?");
} else {
// no user session available, someone you dont know
alert("asd");
}
});
};
采纳答案by Abby
You need to load the api asynchronously. Try removing your <script src="connect.facebook.net/en_US/all.js"></script>
and updating your JS to:
您需要异步加载 api。尝试删除您<script src="connect.facebook.net/en_US/all.js"></script>
的 JS 并将其更新为:
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({
appId : 'YOUR_APP_ID', // App ID
channelURL : '//WWW.YOUR_DOMAIN.COM/channel.html', // Channel File
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
oauth : true, // enable OAuth 2.0
xfbml : true // parse XFBML
});
//
// All your canvas and getLogin stuff here
//
};
// Load the SDK Asynchronously
(function(d){
var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
d.getElementsByTagName('head')[0].appendChild(js);
}(document));
</script>
Oh and check the documentation!
哦,检查文档!
回答by TheRightChoyce
This error will also occur if you don't provide an appId to FB.init. Example:
如果您不向 FB.init 提供 appId,也会发生此错误。例子:
FB.init({
appId: ''
, channelUrl: '//domain/channel.html'
, status: true
, cookie: true
});
Will result in:
会导致:
FB.getLoginStatus() called before calling FB.init().
在调用 FB.init() 之前调用 FB.getLoginStatus()。