-1

有什么想法我在这里做错了吗?

$(document).ready(function(){
    var f_page = "ID";
    var t_page = "ID";

    function add_commas(number) {
        if (number.length > 3) {
            var mod = number.length % 3;
            var output = (mod > 0 ? (number.substring(0,mod)) : '');
            for (i=0 ; i < Math.floor(number.length / 3); i++) {
                if ((mod == 0) && (i == 0)) {
                    output += number.substring(mod+ 3 * i, mod + 3 * i + 3);
                } else {
                    output+= ',' + number.substring(mod + 3 * i, mod + 3 * i + 3);
                }
            }
                return (output);
            } else {
                return number;
        }
    }

    // grab from 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
    $.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
    $.getJSON('json.php?callback=?', function(data) {
        web_count = data['count'].toString();
        web_count = add_commas(web_count);
        $('#website_count').html(web_count);
    });

});

但没有显示在 html 上,但我确实得到了回复 {"count":3} 所以也许 data['count'] 错了?twiiter / facebook 的工作但不是我的计数,就像我在控制台中说的那样,我取回了数组

4

2 回答 2

1

似乎是由于 ?callback=? 我删除了它并且它工作正常

于 2012-12-04T12:49:32.150 回答
-1

您必须解析 JSON 数据

$.getJSON('json.php?callback=?', function(data) {
        var responseObj = $.parseJSON(data);
        website_count = responseObj.count;
        website_count = add_commas(website_count);
        $('#website_count').html(website_count);
    });
于 2012-12-04T12:26:42.997 回答