我正在使用AJAX解析json,但是一切正常,尽管当我尝试调用传递循环索引值的函数,然后函数将其推入Global数组时,似乎没有在推这些值即使console.log()在每一步都按要求打印出所有内容,但是当我检查数组长度时,它始终为0。
//This bit of code is actually inside a function and another ajax success
$.each(updatedData, function(index, element) {
$.ajax({
type: 'GET',
url: 'https://en.wikipedia.org/w/api.php?action=query&prop=extracts&exintro&explaintext&format=json&redirects&callback=?&titles='+updatedData[index],
data: { get_param: 'value' },
dataType: 'json',
success: function (data) {
console.log('success wiki');
//getting the pagenumber key value to use the extract value from it
var pagenum = Object.keys(data.query.pages);
if(data.query.pages[pagenum[0]].extract == null)
{
recycleData(index);
}
}
});
});
//this is the function which should push the trash value to the trashData
//the console.log actually logs all the correct values yet still doesn't push
function recycleData(trash){
console.log("sliced - "+trash);
trashData.push(trash);
}
//that's how the array is defined, it's obviously defined before all the functions just in case someone would ask
var trashData = [];
更新:我已经测试了延迟后的数组长度,在所有ajax请求都完成之后会填充它,但是我需要其他请求来等待此请求完成,以便其他请求将使用更新的数据。此请求是另一个ajax成功,因此请记住这一点。
交互式爱情
饮歌长啸
相关分类