[英]jQuery when() is not working for multiple ajax call
我有一个JavaScript
方法,我们必须调用多个ajax
调用并在所有可用时使用该数据。 在这种情况下,我们使用了jQuery
.when。 但是使用jQuery
when方法接收的数据不同于分别执行时(只是ajax
调用)。
以下是方法调用和接收到的数据。
每当我调用相同的ajax
方法时,数据都会包含所有元素,但是当我使用when方法将其包装在数组中时,数据就会包含所有元素。 您能否让我知道丢失了什么或如何提取数据。
function retailerProfile(){
$("#retailerProfileBody").empty();
$('#retailerProfile').modal('show');
$.when($.ajax({
url: 'products/getAllProductsByCategory.json',
dataType: "json",
type: "POST",
data: {
},
beforeSend: function(xhr) {
xhr.setRequestHeader("Accept", "application/json");
xhr.setRequestHeader("Content-Type", "application/json");
}
}),
$.ajax({
url: 'useroperation/findRetailerByUserName.json',
dataType: "json",
type: "POST",
data: {
},
beforeSend: function(xhr) {
xhr.setRequestHeader("Accept", "application/json");
xhr.setRequestHeader("Content-Type", "application/json");
}
})).done(function( products,data){
console.log( "user :"+data.user);
console.log( "data :"+data);
console.log( "products :"+products);
var profile = '<div id="'+data.id +'">'
+'<div class="well well-sm">'
+'<div class="row">'
+'<div class="col-sm-4 col-md-6"><h4>'+data.user.firstName+' '+data.user.lastName+'</h4>'
+'<address>'
+'<strong>'+data.shopName+'</strong><br/>'
+data.address.addrLine1+'<br/>'
+data.address.addrLine2+'<br/>'
+data.address.city+'<br/>'
+data.address.state+'<br/>'
+'<abbr title="Phone">P:</abbr> '+((!data.user.mobile)?'':data.user.mobile)
+'</address>'
+'<p>'
+'<i class="glyphicon glyphicon-envelope"></i> '+data.user.email
+'<br/><i class="glyphicon glyphicon-map-marker"></i> '+data.address.locality
+'</p>'
+'</div>';
profile+='<div class="col-sm-4 col-md-6"><h4>Products</h4><br/>'
+'<ul class="list-group">';
$.each(data.products,function(k,product){
profile+='<li class="list-group-item">'+ product.productName +'('+product.category +')</li>';
});
profile+='</ul>'
+'</div>'
+'</div>'
+'</div>'
+'</div>'
;
$("#retailerProfileBody").append(profile);
});
}
控制台日志:数据:[对象对象],成功,[对象对象]产品:[对象对象],[对象对象],[对象对象],[对象对象],[对象对象],[对象对象],[对象对象],[对象对象],成功,[对象对象]
差异是由于何时函数行为。 在when函数中,每个参数都是具有以下结构的数组:[data,statusText,jqXHR]。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.