我创建了这个 fiddle .
我已经使用 .when().done() 方法来获取 Facebook 点赞数和 github 关注者的值,但问题是当我将这两个值相加时,我得到了
[object Object],success,[object Object][object Object],success,[object Object]
Jquery
$(function () {
$.when(
$.ajax({
type: "GET",
dataType: "json",
url: "https://api.github.com/users/bloggerever",
success: function (data) {
var githubfollowercount =data.followers;
$(".githubfollowercount").html(githubfollowercount);
}
}),
$.ajax({
type: "GET",
dataType: "json",
url: "http://graph.facebook.com/bloggerever",
success: function (data) {
var facebookfollowcount = data.likes;
$(".facebookfollowercount").html(facebookfollowcount);
}
})).done(function (githubfollowercount, facebookfollowcount) {
var total=facebookfollowcount + githubfollowercount;
$('.totalfollowercount').append(total);
});
});
最佳答案
您将获得与 ajax 调用中相同的响应对象,但在 then()
中它们被包装在数组中,因此您必须首先访问数组中的第一个成员,然后是属性包含喜欢/关注者等
).done(function (git, fb) {
var total = git[0].followers + fb[0].likes;
$('.totalfollowercount').append(total);
});
关于javascript - 多次 AJAX 请求后获取总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23713227/