我有 3 个获取数据的 json 请求,我想存储结果并将它们加在一起?我已经尝试过了,但我得到了 NaN,因为它们被设置为 null。我究竟做错了什么?
var twitter, facebook, web, total_count;
// grab from facebook
var facebook = $.getJSON('https://graph.facebook.com/'+f_page+'?callback=?', function(data) {
var fb_count = data['likes'].toString();
fb_count = add_commas(fb_count);
$('#fb_count').html(fb_count);
});
// grab from twitter
var twitter = $.getJSON('http://api.twitter.com/1/users/show.json?screen_name='+t_page+'&callback=?', function(data) {
twit_count = data['followers_count'].toString();
twit_count = add_commas(twit_count);
$('#twitter_count').html(twit_count);
});
// grab from website
var web = $.getJSON('json.php', function(data) {
web_count = data['count'].toString();
web_count = add_commas(web_count);
$('#website_count').html(web_count);
});
$.when(facebook, twitter, web).done( countTotal );
function countTotal() {
total_count = facebook + twitter + web;
total_count = add_commas(total_count);
$('#count_total').html(total_count);
}