0
我是JS/JQuery的noob。下面的代码:用JSONP填充JSON对象的数组Ajax响应
ids = ["1", "2", "3"]
var imageData = [];
for (i=0; i<ids.length; i++) {
$.ajax({
url: 'http://my.images.edu/' + ids[i] + '/info.json',
type: 'GET',
dataType: 'jsonp',
success: function(data) {
imageData.push(data);
}
});
};
// now I want to do stuff with in a loop with the populated
// array, but it's always empty!
console.log(imageData.length);
的JSONP工作(我可以登录success
功能的JSON对象响应到控制台)。我的猜测based on this question是,当我想使用它时,数组还没有填充,但我可能是错的。如果是这样的话,我该如何解决它,否则,我错过了什么?提前致谢!
嗯......这让我更接近,所以谢谢你。我认为我仍然有两个问题:1.任意ajax调用的数量(基于id的数量),所以不知何故我想在'.when(...)'方法中有一个循环,和2.在成功回调函数中,我需要更新一个全局数组......所以'.then()'(或者完全在块之外)我可以对数组做些什么。 – JStroop 2013-03-13 02:37:13