刷新页面后会显示Facebook帖子:Javascript

时间:2017-10-03 12:37:10

标签: javascript html facebook facebook-graph-api

经过努力,我终于遇到了我想要实现的解决方案。我已经成功地将Facebook的帖子与我的应用程序集成在一起。

但只有一个问题。我希望在登录人员之后在textarea中发布帖子。但除非我刷新网站的页面,否则我看不到帖子。

以下是我要执行的代码:

<div id="fb-root"></div>
<script>
function getUserData() {
    FB.api('/me', {fields: 'email, posts'}, function(response) {
        document.getElementById('for_analysis').innerHTML = 'Hello ' + response.posts.data[0].message;

    });
}

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

    //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 {
            //user is not authorized
            document.getElementById('for_analysis').innerHTML = '';
        }
    });
};


//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.com/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';
            getUserData();
            //document.location.reload(true)
        }
    }, {scope: 'email,user_posts', return_scopes: true});
}, false);

</script>
<div class="fb-login-button" data-max-rows="2" data-size="large" data-button-type="continue_with" data-show-faces="false" data-auto-logout-link="true" data-use-continue-as="true" data-scope = "email,user_posts"></div>

请告诉我,在用户登录后,我需要做些什么才能看到内容。

我尝试使用getUserData()命令执行window.location.reload()后重新加载页面,同时尝试:window.location.reload(true)document.location.reload()document.location.reload(true)

但页面没有停止刷新,因此忽略了重装部分。

请提出更好的解决方案。

1 个答案:

答案 0 :(得分:-1)

您正在使用FB.login和登录按钮。使用您自己的按钮并自行拨打FB.login。现在你甚至没有使用FB.login,只有成功登录后没有任何回调的登录按钮。

或者,您可以使用登录按钮的onlogin回调而忘记FB.loginhttps://developers.facebook.com/docs/facebook-login/web/login-button/#settings

相关问题