我设置了sdk,按钮触发checkLoginState
功能,但FB.login不会被调用。我正在使用最新版本(v2.9)。我也尝试过使用2.8。
我猜这是与异步有关但我无法弄清楚下一步该做什么。
代码:
<script>
window.fbAsyncInit = function() {
FB.init({
appId : 'xxxxxxxxxxx',
status : true,
cookie : true,
xfbml : true,
version : 'v2.8'
});
};
(function(d, s, id){
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) {return;}
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/sdk.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
</script>
<fb:login-button onlogin="checkLoginState();" show-faces="false" scope="email,public_profile" width="400"></fb:login-button>
<script type='text/javascript'>
function checkLoginState() {
FB.getLoginStatus(function(response) {
statusChangeCallback(response);
});
}
function statusChangeCallback(response) {
if (response.status !== 'connected'){
Facebook_login();
}
else{
console.log(response);
}
}
function Facebook_login() {
FB.login(function (response) { <--- debugger comes here
if (response.status === 'connected') { <----- but not here :-(
//do stuff
}
});
}
答案 0 :(得分:1)
登录按钮自行执行登录,您不必在使用该按钮时自己致电FB.login
。
FB.login
用于何时使用您自己的链接/按钮来触发登录。
在你调用它的方式中,在异步方法的回调函数中,它很可能会被浏览器的弹出窗口阻止程序阻止。如果您使用FB.login
,那么您应该只在直接用户交互(即点击链接/按钮)上调用它,而不是将其嵌入到任何异步回调中。
答案 1 :(得分:0)
<span><a onclick="fbLogin()" id="fbLink" class="btn faceebook btn-circle muliregular text-capitalize"><i class="fa fa-facebook"></i> Facebook </a></span>
var global_fb = false;
window.fbAsyncInit = function() {
// FB JavaScript SDK configuration and setup
FB.init({
appId : 'xxxxxxxx', // FB App ID
cookie : true, // enable cookies to allow the server to access the session
xfbml : true, // parse social plugins on this page
version : 'v2.8' // use graph api version 2.8
});
// Check whether the user already logged in
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
//display user data
getFbUserData();
}
});
};
// Load the JavaScript SDK asynchronously
(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/sdk.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
// Facebook login with JavaScript SDK
function fbLogin() {
global_fb = true;
FB.login(function (response) {
if (response.authResponse) {
// Get and display the user profile data
getFbUserData();
} else {
}
}, {scope: 'email'});
}
// Fetch the user profile data from facebook
function getFbUserData(){
FB.api('/me', {locale: 'en_US', fields: 'id,first_name,last_name,email,link,gender,locale,picture'},
function (response) {
$.post('loginwithfacebook',{userData:response},function(data){
if(data.id != ""){
if(global_fb == true){
// do something
}
}
});
});
}