使用facebook javascript api填充表单而不重新加载页面

时间:2016-03-07 15:59:12

标签: javascript facebook sdk

我正在使用facebook api来填充一个from,它看起来工作得很完美,唯一的问题是我只有在刷新页面时才填充表单,有一种方法可以立即使用吗?在这里我的代码:

HTML:

<li><a href="javascript:void();" onclick="fb_login();"><img src="img/fb_login.png" border="0" alt=""></a></li>

JAVASCRIPT

          function statusChangeCallback(response) {
            console.log('statusChangeCallback');
            console.log(response);
            if (response.status === 'connected') {
              testAPI();
            } else if (response.status === 'not_authorized') {
              console.log ('non autorizzato.');
            } else {
              console.log('accedi a facebook');
            }
          }

          function fb_login(){
              FB.login(function(response) {

                  if (response.authResponse) {
                      console.log('Welcome!  Fetching your information.... ');
                      access_token = response.authResponse.accessToken; //get access token
                      user_id = response.authResponse.userID; //get FB UID

                    console.log('access_token: '+access_token+", user_id: "+user_id);

                      FB.api('/me', function(response) {
                          user_email = response.email; //get user email
                      });

                  } else {
                      //user hit cancel button
                      console.log('User cancelled login or did not fully authorize.');

                  }
              }, {
                  scope: 'public_profile,email'
              });
          }
          function checkLoginState() {
            FB.getLoginStatus(function(response) {
              statusChangeCallback(response);
            });
          }


          // ID APPLICAZIONE
          window.fbAsyncInit = function() {
            FB.init({
              appId      : 'myid', //SVILUPPO
              xfbml      : true,
              version    : 'v2.5'
            });

              FB.getLoginStatus(function(response) {
                statusChangeCallback(response);
              });
          };

        // Carico la SDK in asincrono
          (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'));

        //compilo i campi
          function testAPI() {
            console.log('Welcome!  Fetching your information.... ');
            FB.api('/me', {fields:'email,first_name,last_name'}, function(response) {
            console.log(response);
              console.log('Successful login for: ' + response.first_name);
              fb_populate(response)
            });
          }

          function fb_populate(response){
              $('#social').val('facebook_'+response.id);
              $('#nome').val(response.first_name);
              $('#cognome').val(response.last_name);
              $('#email').val(response.email);
          }

1 个答案:

答案 0 :(得分:0)

我的回答可能有点晚,但您可以尝试使用JavaScript SDK中的FB.XFBML.parse();函数。

请参阅:How to force Facebook JS-SDK to render dynamically added widgets?