How to get token in a function? I want send it out of this function for an ajax processing.
<body>
<script src="http://connect.facebook.net/en_US/all.js"></script>
<script>
var token;
window.fbAsyncInit = function() {
FB.init({ appId: 'XXXXXXXX',status: true,cookie: true,xfbml: true,oauth: true});
FB.getLoginStatus(function(response) {
if (response.authResponse){
if(response.status=="connected") {
token = response.authResponse.accessToken;
}
}
});
};
alert(token);//undefined
</script>
<div id="fb-root"></div>
</body>
You’re executing an asynchronous request which may not be finished by the time your alert is executed. To fix your issue, simply call a function when the async method is complete:
Or, simply pass the value in instead of using the
tokenvariable: