[英]jQuery when() is not working for multiple ajax call
I have a JavaScript
method where we have to invoke multiple ajax
calls and use that data when all available. 我有一个
JavaScript
方法,我们必须调用多个ajax
调用并在所有可用时使用该数据。 We have used jQuery
.when for this situation. 在这种情况下,我们使用了
jQuery
.when。 But the data received using jQuery
when method is different from when it is executed separately (just the ajax
call). 但是使用
jQuery
when方法接收的数据不同于分别执行时(只是ajax
调用)。
Below is the method call and data received. 以下是方法调用和接收到的数据。
Whenever I call same ajax
method, data has all the elements but when I call it using when method it is wrapped within an array. 每当我调用相同的
ajax
方法时,数据都会包含所有元素,但是当我使用when方法将其包装在数组中时,数据就会包含所有元素。 Could you please let me know what is missing or how to extract the data. 您能否让我知道丢失了什么或如何提取数据。
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);
});
}
Console Log: data :[object Object],success,[object Object] products :[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],success,[object Object]
控制台日志:数据:[对象对象],成功,[对象对象]产品:[对象对象],[对象对象],[对象对象],[对象对象],[对象对象],[对象对象],[对象对象],[对象对象],成功,[对象对象]
The difference is because of when function behavior. 差异是由于何时函数行为。 in when function each argument is an array with the following structure: [ data, statusText, jqXHR ].
在when函数中,每个参数都是具有以下结构的数组:[data,statusText,jqXHR]。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.