Deferred.done不执行可链接的进程

时间:2016-10-19 12:56:34

标签: jquery ajax jquery-deferred

我阅读了链接https://api.jquery.com/deferred.done/

然后我编辑我的代码:如下

 $.ajax(
        {
            type: 'Get',
            dataType: 'json',
            data: { id: id },
            url: '@Url.Action("GetInsuranceClaim","Insurance")',
            success: function (da) {
                if (da.Message == "Success") {
                    var d = $.Deferred();
                    d.done($('#Cust_id').val(da.Result[0].customer_id), GetPolicy(), $('#policy_no').val(da.Result[0].policy_detail_id), console.log(da.Result[0].policy_detail_id));
  

d.done($('#Cust_id')。val(da.Result [0] .customer_id),GetPolicy(),   $(' #policy_no选项')。val(da.Result [0] .policy_detail_id),   的console.log(da.Result [0] .policy_detail_id));

GetPolicy()是ajax调用我的问题是ajax调用最后执行,

我需要执行订单

  1. $('#CUST_ID&#39)。VAL(da.Result [0] .customer_id)
    1. GetPolicy()
    2. $('#policy_no&#39)。VAL(da.Result [0] .policy_detail_id)

2 个答案:

答案 0 :(得分:1)

尝试下面的代码段 - 请注意在AJAX调用中使用return,使用它非常重要,这样您就可以链接延迟。

注意: AJAX调用也是延期!

$.GetInsuranceClaim = function(){
     return $.ajax({
                type: 'Get',
                dataType: 'json',
                data: { id: id },
                url: '@Url.Action("GetInsuranceClaim","Insurance")',
});

$.GetPolicy = function(){
     //AJAX call - basically the same as the above (don't forget return)
});

$.GetInsuranceClaim().done(function(){
     $.GetPolicy().done(function(policyResponse){
          ///logic you want to do with this response
     }));
}));

答案 1 :(得分:0)

Deferred.done()的参数必须是函数。您当前的代码会立即执行这三个组件,并将三个结果提供给done()方法。

您应该将每个参数包装在函数(){}中。然后done会按顺序调用每个函数。

似乎GetPolicy()最后执行。实际情况是它被称为第二个(在调用done()之前),它只是最后完成,因为它是异步和长的。