如何使用jquery登录facebook用户后检索数据

时间:2016-03-29 11:01:13

标签: javascript ajax facebook laravel-4

我改变了脚本。这是我的代码。  在控制台中我得到了Uncaught ReferenceError:baseUrl没有定义。 我不知道它为什么显示。我应该在这里改变什么。 我需要将数据传递给控制器​​,但它无法正常工作。任何人都可以帮助我的错误。

  <script>
       function testAPI() {
       console.log('Welcome!  Fetching your information.... ');
       var url = '/me?fields=name,email,link';
      FB.api(url, function (response) {
      console.log(response);
      var imgUrl = 'http://graph.facebook.com/' + response.id + '/picture/?                type=large';
         localStorage.setItem("fbImg", imgUrl);
     var fbId = response.id;
    var fbName = response.name;
      var fbEmail = response.email;

     $.ajax({
     url: '<?php echo url('facebooklogin'); ?>',
     type: 'POST',
     data: {facebookId: fbId, username: fbName, email: fbEmail},
    success: function (message)
    {
    console.log(message);
    console.log('good');
    console.log(message.success);
    if (message.success == 1)
    {
    //location.reload();
     //window.location.href = '<?php echo url('babyname/create'); ?>';
    }
    if (message.success == 2)
    {
    location.reload();
    }
    }
    });
   });
    }

  var csrf = '';
  function statusChangeCallback(response) {
  console.log(response);

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

 getFBDetails(response);
  } else if (response.status === 'not_authorized') {

  var c = 'Please log ' + 'into this app.';

  console.log(c);
  } else {

  var c = 'Please log ' + 'into Facebook.';

 console.log(c);
  }
  }

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

   window.fbAsyncInit = function () {
  };
// 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 getFBDetails(resObj) {
 console.log('Welcome!  Fetching fb information.... ');
  FB.api('/me', {locale: 'en_US', fields: 'name, first_name, last_name,  gender, email'}, function (response) {
  var c = 'Thanks for logging in, ' + response.name + ', ' + response.email + ', ' + resObj.authResponse.accessToken;
//document.getElementById('status').innerHTML = c;
  console.log(c);
  console.log(response);
  console.log(resObj.authResponse.accessToken);
  testAPI();
  });
  }

function fb_login() {
FB.init({
appId: '1694320824153828',
    cookie: true,
    xfbml: true,
    version: 'v2.5',
    status: true,
     });
     FB.login(function (response) {
     FB.getLoginStatus(function (response) {
     statusChangeCallback(response);
     });
     }, {scope: 'public_profile,email'});
     }
     </script>

1 个答案:

答案 0 :(得分:0)

添加访问字段,如:

 FB.api('/me', {fields: 'last_name'}, function(response) {
  console.log(response);
});

 FB.api('/me?fields=name,email', function(response) {
  console.log(response);
});

以下是示例代码

    FB.init({
    appId: '1694320824153828',
    cookie: true,
    xfbml: true,
    version: 'v2.5'
});
var statusChangeCallback = function (me) {
    if (me.id) {
        var id = me.id;
        var email = me.email;
        var first_name = me.first_name;
        var last_name = me.last_name;
        var live = '';
        if (me.hometown != null)
        {
            live = me.hometown.name;
        }

        var passData = 'fid=' + id + '&email=' + email + '&first_name=' + first_name + '&last_name=' + last_name + '&live=' + live;
//alert(passData);

// add your ajax code here

    }
};
function fb_login() {
    FB.getLoginStatus(function (response) {
        console.log(response);
        if (response.authResponse) {
            FB.api('/me?field=name,email', statusChangeCallback);
        } else {
            FB.login(function (response) {
                if (response.authResponse) {
                    FB.api('/me?field=name,email', statusChangeCallback);
                } else {
                    //user cancelled login or did not grant authorization
                }
            }, {scope: 'email'});
        }
    });
}