我正在使用 facebook API 开发应用程序。 我正在尝试的很简单 - 获取一个帖子,然后获取该帖子的点赞数。
然后我想将帖子故事及其点赞数结合起来。 稍后我会想用一个循环来完成它 - 迭代一系列帖子。
我的问题解释在代码中:
$(function () {
$("#mybutton").click(function () {
FB.api("/me/posts", function (response) {
/* first callback
*
* i have access to global scope here.
*/
var firstpost = response.data[0];
FB.api("/" + firstpost.id + "/likes", function (response) {
/* second callback inside the first callback
*
* here i lost my access to the first callback scope.
* my access is only to local and global scope.
* i can't see anymore what is inside firstpost variable;
*/
console.log(firstpost);
//this will give a reference error
})
})
})
});
也许有更好的方法?
最佳答案
您可以对响应参数使用不同的名称,但有一种更好的方法,只需使用一个 API 调用即可:
/me/posts?fields=message,likes
...或使用 JS SDK:
FB.api('/me/posts', {fields: 'message,likes'}, function (response) { ... }
关于javascript - facebook api - 失去我的范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41640004/