Getting userdata using Facebook Login Javascript S

2019-09-14 23:08发布

问题:

I've used the Javascript SDK to use Facebook login, but now I'm connected, I don't know how to capture the user's data and send it on a codeigniter controller. I decided to use ajax, but I don't know how to implement it if I still don't have my facebook userdata.

if (response.status === 'connected') {

//How do I get userdata??
// I decided to use ajax

$.ajax({
    type : 'POST',
    data : 'userdata='+ //but I don't know how I would put my data,
    url : '<?php echo site_url("somecontroller/somefunction");?>',
    success :   function(){

    }
});

}

Also, how do I get the data to be placed in an array so I could just use 1 variable when sending it to my controller?

回答1:

After you verify if user is connected you access facebook api for curent user :

FB.api('/me', function(info) {
//for example facebook id for curent user 
var id = info.id;
//you can view all data which you can use, if you need more data ask for more permisson
console.log(info);

//here you can ajax data for your need

$.ajax({
    type : 'POST',
    data : info, //all data 
    url : '<?php echo site_url("somecontroller/somefunction");?>',
    success :   function(){

    }
});

//or only date which you need

$.ajax({
  type: "POST",
  url: '<?php echo site_url("somecontroller/somefunction");?>',
  data: { id: info.id, fullname: info.name },
   success :   function(){

    }
})

});


回答2:

if (response.status === 'connected') {
    var userId = response.authToken.userID;
    FB.api('/'+userId, function(response) {
     // response contain user's public data
   }); 
}

Although, if u want to get more details than public data, u have to get the user permission using FB.login