我有一个函数,可以进行 2 个 getjson 调用并将响应写入数组。我在第二次 getjson 调用结束时使用了以下代码:
alert(files.length);
print_r(files);
console.log(files);
但是,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。
关于javascript - 如何在 getjson 调用结束时显示数组内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32889072/