FB.getSession() returns null
In one of my application I am using client side Facebook javascript api to track user's facebook login status.
I have got user already connected with facebook application, and user is logged in to the facebook.
Now when I call开发者_开发问答 FB.getSession() it returns null, on calling FB.getLoginStatus() also does not give me response.session....
This behavior is different in different browser, it works fine in chrome but not in firefox 4. I cleared the cookies in firefox still not working as expected.
Is this client side api trusted or should i opt for server side api?
code:
window.fbAsyncInit = function() {
FB.init({
appId: '0000000000',
status: true,
cookie: true,
xfbml: true
});
FB.getLoginStatus(function(response) {
if (response.session) {
// This block should have been called.
alert('logged in')
} else {
// no user session available, someone you dont know
alert('not logged in')
}
}, true);
var session = FB.getSession(); //Must not return null...
};
Any idea?
It worked after using non async approch.
Code
<script src="http://connect.facebook.net/en_US/all.js"></script>
<script>
FB.init({
appId : '00000000000',
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
$(function(){
//var session = FB.getSession();
alert(FB.getSession());
})
</script>
No idea though why didnt it worked with an async approach.
It's simply because FB.getSession() isn't initialized.
You could't add an event to the FB.getLoginStatus which fires the FB.getSession() functionality when Facebook is initialized and thereby use the async approach.
精彩评论