2016-06-20 179 views
1

我使用AJAX請求successives,我需要做一個回調時,所有的successives請求完成雙Ajax請求的響應

function doAjaxRequest(data, id) { 
    // Get payment Token 
    return $.ajax({ 
     type: "POST", 
     url: 'exemple1.php', 
     data: data 
     success: function(msg){ 
      $.ajax({ 
       type: "POST", 
       url: 'exemple2.php', 
       data: msg, 
       success: function(msgr) { 
        document.getElementById(id).value=msgr; 
       }, 
       error:function (xhr, status, error) { 
        //Do something 
       } 
      }); 
     }, 
     error:function (xhr, status, error) { 
      //Do something 
     } 
    }); 
} 

$.when(
    doAjaxRequest(data, "input-1"), 
    doAjaxRequest(otherData, "input-2") 
).done(function(a1, a2){ 
    //Need do something when both second ajax requests (example2.php) are finished 
} 

有了這個代碼,該完成的功能是之前我的電話給「exemple2電話。 PHP「成功。

我該如何等待?

感謝您的回答!

回答

1
function doAjaxRequest(data, id) { 
    // Get payment Token 
    return new Promise(function(resolve,reject){ 
     $.ajax({ 
     type: "POST", 
     url: 'exemple1.php', 
     data: data 
     success: function(msg){ 
      $.ajax({ 
       type: "POST", 
       url: 'exemple2.php', 
       data: msg, 
       success: function(msgr) { 
        document.getElementById(id).value=msgr; 
        resolve(); 
       }, 
       error:function (xhr, status, error) { 
        //Do something 
        reject(); 
       } 
      }); 
     }, 
     error:function (xhr, status, error) { 
      //Do something 
      reject(); 
     } 
    }); 
    }); 
} 



Promise.all([ 
    doAjaxRequest(data, "input-1"), 
    doAjaxRequest(otherData, "input-2")]) 
.then(function(values){ 
    //Need do something when both second ajax requests (example2.php) are finished 
} 
+0

糾正我,如果我錯了,但IE不支持它,對吧? –

+0

https://api.jquery.com/category/deferred-object/在IE上支持,具有相同的邏輯。 謝謝! – Bouffe

0

返回自定義對象推遲,e.g:

function doAjaxRequest(data, id) { 
    var d = new $.Deferred(); 
    // Get payment Token 
    $.ajax({ 
     type: "POST", 
     url: 'exemple1.php', 
     data: data 
     success: function(msg){ 
      $.ajax({ 
       type: "POST", 
       url: 'exemple2.php', 
       data: msg, 
       success: function(msgr) { 
        document.getElementById(id).value=msgr; 
        d.resolveWith(null, [msgr]); // or maybe d.resolveWith(null, [msg]); 
       }, 
       error:function (xhr, status, error) { 
        //Do something 
        d.reject(); 
       } 
      }); 
     }, 
     error:function (xhr, status, error) { 
      //Do something 
      d.reject(); 
     } 
    }); 
    return d; 
} 

現在,我不知道什麼是傳遞給$.when().done()回調您的預計DATAS。

1

你的子Ajax請求是promise.abort 剛剛嘗試用事實第一AJAX結果的獨立的,然後示例2呼叫被完全從$。當()分隔的jQuery的$就返回答應狀物體 這裏我的代碼從plnkr

// Code goes here 
function doAjaxRequest(data, id) { 
    // Get payment Token 
    return $.ajax({ 
     type: "GET", 
     url: 'example1.json', 
     data: data 
    }).then(function(msg, status, jqXhr) { 
     return $.ajax({ 
      type: "GET", 
      url: 'example2.json', 
      data: msg 
     }); 
    }).done(function(msgr) { 
     console.log(msgr); 
     return msgr; 
    }); 
} 

var data = {foo:'bar'}; 
var otherData = {foo2:'bar2'}; 

$.when(
    doAjaxRequest(data, "input-1"), 
    doAjaxRequest(otherData, "input-2") 
).done(function(a1, a2) { 
    console.log(a1, a2); 
    //Need do something when both second ajax requests (example2.php) are finished 
}); 

注意,我更換POST通過GET和plnkr使用exampleX.json文件我的測試

你可以在這裏進行測試:https://plnkr.co/edit/5TcPMUhWJqFkxbZNCboz