我使用facebook JavaScript sdk登录功能。 但FB.api(' / me',功能(响应))没有按预期工作。 当我完成点击登录按钮后,它应该在div中显示我的个人资料信息(id = status) 但它第一次没有显示该信息,但是当我刷新页面时它会显示出来。所以我希望它在div中显示一旦我完成登录按钮点击而无需刷新或重新加载页面。 下面是我的脚本代码:
function statusChangeCallback(response)
{
console.log('statusChangeCallback');
console.log(response);
if (response.status === 'connected')
{
testAPI();
}
else if (response.status === 'not_authorized')
{
document.getElementById('status').innerHTML = 'Please log ' +'into this app.';
}
else
{
document.getElementById('status').innerHTML = 'Please log ' +'into Facebook.';
}
}
function checkLoginState()
{
FB.getLoginStatus(function(response)
{
statusChangeCallback(response);
});
}
window.fbAsyncInit = function()
{
FB.init({
appId : 'MY_APP_ID',
cookie : true, // enable cookies to allow the server to access
// the session
xfbml : true, // parse social plugins on this page
version : 'v2.2' // use version 2.2
});
FB.getLoginStatus(function(loginResponse)
{
statusChangeCallback(loginResponse);
});
};
function Logout()
{
FB.logout(function(){document.location.reload();});
}
function getPhoto()
{
FB.api('/me/picture?type=normal', function(response)
{
var str="<br/><b>Pic</b> : <img src='"+response.data.url+"'/>";
document.getElementById("status").innerHTML+=str;
});
}
var myWindow=null;
function fblogin()
{
FB.login(function(response)
{
console.log('FB.login response',response);
checkLoginState();
});
return false;
}
(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'));
function testAPI()
{
console.log('Welcome! Fetching your information.... ');
FB.api('/me', function(response)
{
console.log('Successful login for: ' + response.name);
var str="<b>Name</b> : "+response.name+"<br>";
str +="<b>Link: </b>"+response.link+"<br>";
str +="<b>Username:</b> "+response.username+"<br>";
str +="<b>id: </b>"+response.id+"<br>";
console.log(response);
str +="<b>Email:</b> "+response.email+"<br>";
str +="<input type='button' value='Get Photo' onclick='getPhoto();'/>";
str +="<input type='button' value='Logout' onclick='Logout();'/>";
document.getElementById('status').innerHTML =str;
'Thanks for logging in, ' + response.name + '!';
});
}
所以上面的代码片段有时会调用,但不能无缝调用。 请帮忙