Javascript 使用Graph API获取信息时遇到问题
我正在尝试使用Facebook Graph API获取通过Facebook登录的用户的信息 一开始,我只使用了一个函数,没有出现错误;然后,我添加了另一个使用Facebook API的函数。从那一刻起,所有功能都不起作用。只有当我删除新函数时,旧函数才能再次工作 我想代码比我上面描述的要容易理解得多:Javascript 使用Graph API获取信息时遇到问题,javascript,jquery,facebook,facebook-graph-api,facebook-javascript-sdk,Javascript,Jquery,Facebook,Facebook Graph Api,Facebook Javascript Sdk,我正在尝试使用Facebook Graph API获取通过Facebook登录的用户的信息 一开始,我只使用了一个函数,没有出现错误;然后,我添加了另一个使用Facebook API的函数。从那一刻起,所有功能都不起作用。只有当我删除新函数时,旧函数才能再次工作 我想代码比我上面描述的要容易理解得多: function fb_login(){ FB.login(function(response) { if (response.authRespo
function fb_login(){
FB.login(function(response) {
if (response.authResponse) {
access_token = response.authResponse.accessToken; //get access token
user_id = response.authResponse.userID; //get FB UID
FB.api('/me', function(response) {
user_email = response.email; //get user email
$.ajax({
type: "POST",
url: "http://*******/ajax.php",
data: { action: "login", id: user_id, email: user_email, first_name: response.first_name, last_name: response.last_name }
}).done(function( msg ) {
alert( "Data Saved: " + msg );
});
});
} else {
//user hit cancel button
console.log('User cancelled login or did not fully authorize.');
}
}, {
scope: 'publish_actions,email,read_stream,publish_stream'
});
}
除非存在以下功能,否则上述功能将正常工作
function update_autopost(form)
{
FB.api('/me', function(response) {
var autopost_value = form.autopost.checked;
if(autopost_value == false)
{
autopost_value = "off";
}
else{
autopost_value = "on";
}
$.ajax({
type: "POST",
url: "http://*********/ajax.php",
data: { action: "update", id: response.authResponse.userID, autopost: autopost_value }
}).done(function( msg ) {
alert( "Data Saved: " + msg );
});
});
}
更详细地说,添加第二个函数后,会发生两件事:
第一个函数停止工作。
第二个函数无法从Facebook Graph API获取信息,例如response.authResponse.userID不起作用。
可能form.autopost.checked1有问题,请使用浏览器控制台检查错误2检查Pulkit注释3可能第二个函数中的图形调用没有返回有效响应,因此使用与第一个函数中相同的验证:如果响应{…此处的代码}oh/me没有返回response.authResponse,尝试在JS中测试它。您应该使用类似response.idI的东西来获取此错误Chrome控制台:Uncaught TypeError:无法读取未定义的属性“userID”这意味着什么?我也尝试过你的建议,但仍然不起作用……这意味着你试图访问一个名为userID的属性的对象实际上不是一个对象,而是“nothing”,在JavaScript中称为undefined。调用/me不会给您一个authResponse对象,只有FB.login/FB.getLoginStatus会给您。