需要从Facebook响应中检索属性

时间:2015-07-10 00:53:48

标签: javascript jquery arrays facebook-javascript-sdk

您好我正在尝试从Facebook登录的响应中检索一些数据,但我没有成功返回这些值,这是我制作的代码:

function loginfb(){
    var resp=[];

    FB.login(function(response,resp) {

        //        console('response:' + response.status);
        if (response.status === 'connected'){
            swal({
                title:'Connecté',
                text: "Vous êtes connecté avec votre compte Facebook",
                type: "success",
                confirmButtonColor: "#81d8d0"
            });                   

            getName();
            resp = [response.status,response.email,response.name];

        }else{
            swal("Erreur", "Nous n'avons pas pu vous conecter à votre compte facebook, désolé", "error");
            resp = [response.status];
        }
    });

    console.log('statusssss: ' + resp[1]);
}


window.fbAsyncInit = function initfb() {
    FB.init({
        appId      : 'XXXXXXXXXXXXXX',
        cookie     : true,  // enable cookies to allow the server to access 
                            // the session
        xfbml      : true,  // parse social plugins on this page
        version    : 'v2.3' // use version 2.2
    });
}; 

// Load the SDK asynchronously
(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'));

// Here we run a very simple test of the Graph API after login is
// successful.  See statusChangeCallback() for when this call is made.
function getName() {
    //    console.log('Welcome!  Fetching your information.... ');
    FB.api('/me', function(response) {
        document.getElementById('status').innerHTML =       'Bienvenue, ' +        response.name;
    });
}

用户已连接登录阶段,但我无法返回响应值以便稍后在代码上处理它。 请帮忙。

1 个答案:

答案 0 :(得分:0)

最后这是我做的代码,它对我来说很好用:

function loginfb(){


FB.login(function(response) {


//        console('response:' + response.status);
if (response.status === 'connected'){

       FB.api('/me', function(response) {

  swal({
        title:'Connecté',
        text: "Vous êtes connecté avec votre compte Facebook",
        type: "success",
        confirmButtonColor: "#81d8d0"
            },function(){


  document.cookie="userCookie=99999999;path=/";       
  document.cookie="userName="+ response.name + ";path=/";           
  document.getElementById('status').innerHTML =       'Bienvenue, ' + response.name; 
  location.reload();

   });  



  });

  }else{
    swal("Erreur", "Nous n'avons pas pu vous conecter à votre compte facebook, désolé", "error");

  }


  });





}


window.fbAsyncInit = function initfb() {
FB.init({
appId      : 'XXXXXXXXXXXXXXXXXX',
cookie     : true,  // enable cookies to allow the server to access 
                    // the session
xfbml      : true,  // parse social plugins on this page
version    : 'v2.3' // use version 2.2
 });


 };

 // Load the SDK asynchronously
(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'));