getLoginStatus javascript页面重新加载时消失 [英] getLoginStatus javascript facebook disappear when page reload

查看:157
本文介绍了getLoginStatus javascript页面重新加载时消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现了facebook连接,工作正常。

I implement the facebook connect and it works fine.

此函数在登录后返回我的名字。

This function return my name after login.

FB.getLoginStatus(function (response) {
    if (response && response.status === 'connected') {
         FB.api('/me', function(response) {
            console.log('Good to see you, ' + response.name + '.');
        });
        //console.log('connected');
    } else {
        console.log('not connected with FB');
    }
});  

但是,当我重新加载页面时,我记得这个功能,它返回没有与FB连接

But, when I reload the page, I recall this function and it returns 'not connected with FB'.

我加入我的登录功能来帮助:

I join my login function to help :

    FB.login(function(response) {

    if (response.authResponse){
      //L'utilisateur a autorisé l'application
      //window.location.replace("http://adresse/de/redirection");
    } else {
      //L'utilisateur n'a pas autorisé l'application
    }
  }, {scope: 'public_profile,user_friends,email'});

我在Facebook文档中找不到任何解决方案

I don't find any solution in facebook documentation

为了显示问题,我的控制台日志如下所示:
控制台日志与我的代码

To show the issue, my console log is like this: console log with my code

编辑:
请查找附加我的完整代码。我从localhost处理

EDIT : Please find attach my full code. I process from localhost

<!DOCTYPE html>
<html>
<head>
<title>Facebook Login JavaScript Example</title>
<meta charset="UTF-8">

</head>
<body>

<button id="loginBtn">Facebook Login</button>
<div id="response"></div>

</body>
<script>

function getUserData() {
    FB.api('/me', function(response) {
        document.getElementById('response').innerHTML = 'Hello ' + response.name;
    });
}

window.fbAsyncInit = function() {
    //SDK loaded, initialize it
    FB.init({
        appId      : 'XXXX',
        xfbml      : true,
        version    : 'v2.2'
    });

    //check user session and refresh it
    FB.getLoginStatus(function(response) {
        if (response.status === 'connected') {
            //user is authorized
            document.getElementById('loginBtn').style.display = 'none';
            getUserData();
        } else {
            console.log(response.status);
            //user is not authorized
        }
    });
};

//load the JavaScript SDK
(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'));

//add event listener to login button
document.getElementById('loginBtn').addEventListener('click', function() {
    //do the login
    FB.login(function(response) {
        if (response.authResponse) {
            //user just authorized your app
            document.getElementById('loginBtn').style.display = 'none';
            console.log(response);
            getUserData();
        }
    }, {scope: 'public_profile,user_friends,email', return_scopes: true});
}, false); 
</script>
</html>

感谢支持

推荐答案

对于任何人阅读这个页面(像我这样做),正确的答案是作为评论发布的。

For anyone reading this page sloppily (as I did), the correct answer was posted as a comment to the question.

解决方案是在调用FB.init中的选项对象中添加 cookie:true

The solution is to add cookie: true to the options object in your call to FB.init.

这篇关于getLoginStatus javascript页面重新加载时消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆