我在尝试通过 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');
}
});
});
});
});
在 App Center 权限中,我还为电子邮件配置了用户和好友权限。似乎没有任何效果。我错过了什么吗?
最佳答案
您将范围参数作为参数传递给 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'});
});
});
});
关于javascript - 尽管使用范围变量,FB Javascript API 不返回电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21221336/