我正在尝试通过 fb.api() 调用获取所有带有评论者个人资料图片的 Facebook 评论。目前,所有评论都会输出,我只是无法将个人资料图片附加到适当的评论中。
在循环所有评论的 for 循环中,有另一个 FB.api 调用,它获取对视频发表评论的用户的个人资料图片,然后将其附加到单个评论 block 中。通过此代码,我可以获得在每条评论中发表评论的所有用户的个人资料图片。 我究竟做错了什么??预先感谢您!
var komentarji_length = response.comments.data.length;
for (var i = 0; i < komentarji_length; i++) {
var user_id = response.comments.data[i].from.id;
FB.api("/" + user_id + "/picture", {
access_token: access_token
}, function (response) {
var profile_pic_link = response.data.url;
$(".comments_profile_pic" + i).append("<img src=" + comments_profile_pic_link + ">");
});
var ime = response.comments.data[i].from.name;
var message = response.comments.data[i].message;
$(".fb_comments").append('<div class="single_comment"><div class="comments_profile_pic' + i + '"></div><div class="name">' + name + '  says:</div><div class="single_message">' + message + '</div></div>');
}
最佳答案
问题 1:comments_profile_pic_link
未定义,变量为:profile_pic_link
。
但是这样问题也不会得到解决。当您调用 "/"+ user_id + "/picture"
时,它会自动重定向到图像网址。因此,您可以直接使用图像网址:https://graph.facebook.com/{user-id}/picture
。不需要进行不必要的API调用。
不过,如果您希望获得正确的图像网址(尽管不是必需的),请在调用中使用 redirect=0
-
FB.api("/" + user_id + "/picture?redirect=0", {
access_token: access_token
}, function (response) {
var profile_pic_link = response.data.url;
$(".comments_profile_pic" + i).append("<img src=" + comments_profile_pic_link + ">");
});
关于javascript - for循环中调用FB api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23174890/