我正在尝试进行以下简单的facebook集成:
我只想阅读以前授权的应用状态,无论是已连接还是未授权,我都希望处理结果。
但我收到错误FB.getLoginStatus() called before calling FB.init()
。在我调用.init
时,假设getLoginStatus
没有完成,这是有道理的。
如何在不添加异步动态js引用的情况下解决此问题?
<script language="javascript">
$(document).ready(function () {
window.fbAsyncInit = function () {
FB.init({
appId: 'myappid', // App ID
status: true, // check login status
cookie: true, // enable cookies to allow the server to access the session
xfbml: true // parse XFBML
});
};
FB.getLoginStatus(function (response) {
alert(response.status);
if (response.status == "connected") {
$("#lkLogin").css("display", "none");
$("#spUser").css("display", "inline");
$("#spUser").html(response.name);
} else if (response.status == "not_authorized") {
$("#lkLogin").css("display", "inline");
$("#spUser").css("display", "none");
}
});
});
</script>
<a href="#" id="lkLogin" style="display:inline;">Facebook Login</a>
<span id="spUser" style="display:none;"></span>
答案 0 :(得分:0)
您应该在致电FB.getLoginStatus()
后立即致电FB.init()
函数;因为init()
被称为asynch。所以发生的事情是FB
在getLoginstatus()
被调用之前没有被初始化;所以你应该像这样叫它同步 -
window.fbAsyncInit = function () {
FB.init({
appId: 'myappid', // App ID
status: true, // check login status
cookie: true, // enable cookies to allow the server to access the session
xfbml: true // parse XFBML
});
// call FB.getloginstatus function here
};
答案 1 :(得分:-2)
这应该这样做:
<script language="javascript">
window.fbAsyncInit = function () {
FB.init({
appId: 'myappid', // App ID
status: true, // check login status
cookie: true, // enable cookies to allow the server to access the session
xfbml: true // parse XFBML
});
};
$(document).ready(function () {
FB.getLoginStatus(function (response) {
alert(response.status);
if (response.status == "connected") {
$("#lkLogin").css("display", "none");
$("#spUser").css("display", "inline");
$("#spUser").html(response.name);
} else if (response.status == "not_authorized") {
$("#lkLogin").css("display", "inline");
$("#spUser").css("display", "none");
}
});
});
</script>
<a href="#" id="lkLogin" style="display:inline;">Facebook Login</a>
<span id="spUser" style="display:none;"></span>