Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/89.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 处理两个ajax请求中只有一个成功执行的情况_Javascript_Jquery - Fatal编程技术网

Javascript 处理两个ajax请求中只有一个成功执行的情况

Javascript 处理两个ajax请求中只有一个成功执行的情况,javascript,jquery,Javascript,Jquery,我提出了两个ajax请求。我能够处理使用jquery延迟对象成功执行两个ajax请求的情况。现在我想处理一个成功,另一个失败的情况 usedCarPurchaseInqPromise和hitrecapi是两个jquery延迟对象 代码: var usedCarPurchaseInqPromise = D_buyerProcess.buyerProcessApis.processUsedCarPurchaseInquiries(boxObj, leadData); usedCarPurchaseI

我提出了两个ajax请求。我能够处理使用jquery延迟对象成功执行两个ajax请求的情况。现在我想处理一个成功,另一个失败的情况

usedCarPurchaseInqPromise
hitrecapi
是两个jquery延迟对象

代码

var usedCarPurchaseInqPromise = D_buyerProcess.buyerProcessApis.processUsedCarPurchaseInquiries(boxObj, leadData);
usedCarPurchaseInqPromise.always(function () {
    if (isFromCaptcha == "1")
        isFromCaptcha = "0";
    if (isGSDClick == "1")
        isGSDClick = "0";
    hideBuyerForm(boxObj);
    $('#newLoading').hide();
    boxObj.find('#pg-process_img,#pg-loadingImg').hide();
    boxObj.find("#loadDetails").hide();
}).fail(function () {
    D_buyerProcess.sellerDetails.processLeadFailureResponse(boxObj, jqXHR);
});
var hitRecoApi = $.ajax({
    type: 'GET',
    url: stockRecommendationsUrl_g,
    headers: { "sourceid": "1" },
    dataType: 'json'
});

$.when(usedCarPurchaseInqPromise, hitRecoApi).done(function (response, jsonData) {
    Common.utils.lockPopup();
    if (D_buyerProcess.utils.isNumberChanged(buyersMobile)) {//if user has changed number as compared to previously stored number in cookie, remove the finance offer of previous number
        if (typeof (customerFinanceUI) !== 'undefined' && typeof (customerFinance) !== 'undefined') {
            customerFinanceUI.hideFinanceOffer();
            customerFinance.removeFinanceOffer(); // removes finance cookie and storage
        }
    }
} 

您可以做的是将您的
$.ajax
调用包装到一个函数中,该函数返回一个承诺,并按如下方式处理:

let p1 = promiseAjax1()
           .then(result => {result: result, err: null})
           .catch(err => {result: null, err: err})

let p2 = promiseAjax2()
           .then(result => {result: result, err: null})
           .catch(err => {result: null, err: err})

Promise.all([p1, p2])
    .then(results => {
         p1response = results[0];
         p2response = results[1];
         // here you can check for errors in p1response or p2response, by checking eg if p1response.err == null
    })

在这种情况下,处理来自$.whenI的“fail”回调。当usedCarPurchaseInqPromise成功而hitRecoApi失败时,您可以编写一个小片段。不管哪一个失败,您都可以处理.fail(函数(){在$.when()调用上)。或者将成功/失败回调组合在一个“then”中正如您发送的链接末尾的示例所示,当任何一个链接失败时,都会起作用。我特别希望第一个通过,第二个失败。文档中说“传递给failCallbacks的参数与被拒绝的延迟的failCallback的签名相匹配。”如果您需要对这种情况执行额外的处理,例如取消任何未完成的Ajax请求,您可以在闭包中保留对底层jqXHR对象的引用,并在failCallback中检查/取消它们。”因此,您需要做一些工作,但您应该能够找出哪一个失败了。