如何在getjson调用结束时显示数组内容

How to display array content at the end of getjson call?

本文关键字:显示 数组 结束 getjson 调用      更新时间:2023-09-26

我有一个函数,使2 getjson调用和写入响应数组。我在第二次getjson调用结束时使用了以下代码:

alert(files.length);
print_r(files);
console.log(files);

然而,文件。Length告诉我数组中项目的数量但是print_r(files);console.log不工作?我想打印数组项目,以确认我得到了正确的项目,但数组打印不工作!谁能告诉我如何解决这个问题?(我的目标是以后排序这个数组和删除重复和过滤…)

    <script>
    var files = new Array();
       function pushtoArray(){
    //first getjson call
    var url1 = "https://spreadsheets.google.com/feeds/list/xxxxx/xxxxx/public/values?alt=json"; 
        $.getJSON(url1, function(data) {
          var entry = data.feed.entry;
          $(entry).each(function(){
            // Column names are name, age, etc.
        count++;
         files.push({ url: this.gsx$url.$t, filename: this.gsx$name.$t });
            $('.results').prepend('<h2>'+this.gsx$name.$t+'</h2><p>'+this.gsx$url.$t+'</p>');
          });
        alert(files.length);
        print_r(files);
        console.log(files);
        });//end of ajax call
    //second getjson call
var url2 = "https://spreadsheets.google.com/feeds/list/xxxxx/xxxxx/public/values?alt=json"; 
        $.getJSON(url2, function(data) {
          var entry = data.feed.entry;
          $(entry).each(function(){
            // Column names are name, age, etc.
        count++;
         files.push({ url: this.gsx$url.$t, filename: this.gsx$name.$t });
            $('.results').prepend('<h2>'+this.gsx$name.$t+'</h2><p>'+this.gsx$url.$t+'</p>');
          });
        alert(files.length);
        print_r(files);
        console.log(files);
        });//end of ajax call

        };//end of function
    </javascript>
html代码:

<body onload="pushtoArray()">

Print_r是一个PHP函数,在javascript中你只能使用console.log.