Javascript 如何打印延迟对象数组中的值?
我有以下要求:Javascript 如何打印延迟对象数组中的值?,javascript,jquery,ajax,jquery-deferred,Javascript,Jquery,Ajax,Jquery Deferred,我有以下要求: var req1 = $.ajax({ type: "GET", url: url, dataType : "xml" }); req1.done(function (resp1) { $(resp1).find('interest').each(function() {
var req1 = $.ajax({
type: "GET",
url: url,
dataType : "xml"
});
req1.done(function (resp1) {
$(resp1).find('interest').each(function() {
var interest_id = $(this).find('id').text();
var interest_name = $(this).find('name').text();
var request = $.ajax({
type:"GET",
url: "http://en.wikipedia.org/w/api.php?action=parse&format=json&page="+ interest_name + "&redirects&prop=text",
dataType: "jsonp"
});
requestsArray.push(request);
});
$.when.apply(null, requestsArray).done(function () {
console.log("entrou");
});
});
但是当我进入
$.when.apply(null, requestsArray).done(function () {
console.log("entrou");
});
我不知道如何在requestsArray中获得个人回复。我该怎么做?我试了又试,但似乎没有任何效果。您可以使用
参数
对象访问传递给函数的未知数量的值:
$.when.apply($, requestsArray).done(function(){
console.log(arguments); //plain arguments object
console.log([].slice.call(arguments)); //arguments turned into real array
});
看
既然你把<代码> $ .ajax < /c> > s放入你的数组中,你可能会考虑去掉额外的参数,比如:
var request = $.ajax({
type:"GET",
url: "http://en.wikipedia.org/w/api.php?action=parse&format=json&page="+ interest_name + "&redirects&prop=text",
dataType: "jsonp"
}).then(function(data, textStatus, jqXHR){
return data; //this will make sure textStatus and jqXHR aren't passed any further
});
[]将是我的数组请求吗?但是像那样,我不会在执行时完成所有请求then@João,不,这只是将arguments对象转换为真实数组的最短方法:-您的
requestsArray
将只是requestsArray
@João您怎么会这么认为?@João这两个代码块组合在一起。第二个是防止附加数据(textStatus
,jqXHR
)出现在参数中。