尽管使用范围变量,FB Javascript API仍未返回电子邮件

Kre*_*ton 2 javascript facebook facebook-javascript-sdk

我正在努力通过FB.login检索电子邮件.这是我的代码如下:

$(document).ready(function() {
        $.ajaxSetup({ cache: true });
        $.getScript('//connect.facebook.net/en_US/all.js', function(){
          FB.init({
            appId: 'appId',
                    cookie: true,
                    oauth: true
          });     

          $('#fbRegister').on('click', function(){
              FB.login(function(response){
                  if(response.authResponse)
                  {
                      FB.api('/me?fields=email,name', function(responseFromFB){                                                           

                          var name = responseFromFB.name; 
                          var email = responseFromFB.email;

                          //Take the vales and send to 
                          $.ajax({

                                type: "POST",
                                url: 'php/register.php',        
                                async: false,
                                data: {'name':name,'email': email},
                                success: function(data)
                                {
                                   $('#fbRegister').hide();
                                   $('#successPrompt').show().text(data);

                                },
                                complete: function() {},
                                error: function(xhr, textStatus, errorThrown) 
                                {
                                  console.log('ajax loading error...');
                                  return false;
                                }
                         });

                      },{scope:'email,name'});
                  }
                  else
                  {
                      console.log('The login failed because they were already logged in');
                  }
              });                  
          });
        });
      }); 
Run Code Online (Sandbox Code Playgroud)

在App Center权限中,我还为电子邮件配置了"用户和朋友"权限.似乎没有什么工作.我错过了什么吗?

Kuk*_*won 7

您将作用域参数作为参数传递给FB.api方法.它们需要提供给FB.login方法.试试这个:

$(document).ready(function() {
    $.ajaxSetup({ cache: true });
    $.getScript('//connect.facebook.net/en_US/all.js', function(){
      FB.init({
        appId: 'appId',
                cookie: true,
                oauth: true
      });     

      $('#fbRegister').on('click', function(){
          FB.login(function(response){
              if(response.authResponse)
              {
                  FB.api('/me?fields=email,name', function(responseFromFB){                                                           

                      var name = responseFromFB.name; 
                      var email = responseFromFB.email;

                      //Take the vales and send to 
                      $.ajax({

                            type: "POST",
                            url: 'php/register.php',        
                            async: false,
                            data: {'name':name,'email': email},
                            success: function(data)
                            {
                               $('#fbRegister').hide();
                               $('#successPrompt').show().text(data);

                            },
                            complete: function() {},
                            error: function(xhr, textStatus, errorThrown) 
                            {
                              console.log('ajax loading error...');
                              return false;
                            }
                     });

                  });
              }
              else
              {
                  console.log('The login failed because they were already logged in');
              }
          }, {scope:'email,public_profile'});                  
      });
    });
  }); 
Run Code Online (Sandbox Code Playgroud)