繁体   English   中英

jQuery when()不适用于多个ajax调用

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM