简体   繁体   English

是否存在可以在ajax请求中调用成功和错误回调函数的情况?

[英]Is there a scenario in which the success & error callback function could be called within an ajax request?

In below pseudo-code the error a redirect is occurring even though the error callback is being invoked. 在下面的伪代码中,即使正在调用错误回调,也会发生错误重定向。 The success callback should not be called if the error call back is called ? 如果调用了错误回调,则不应调用成功回调。

$.ajax({

success : {

redirectPage();
}

error : {

}

Either the success callback is called, or the error one, but not the both at the same ajax request. 是调用成功回调,还是调用错误1,但不会在同一ajax请求中调用两者。

That said, if you need to fake this behavior, here could be a solution: 也就是说,如果您需要伪造此行为,则可以采取以下解决方案:

// first solution

$.ajax({
    complete: function() {
        alert("complete() called...");
    }
};

// second solution

function success() {
    alert("success() called...");
}

$.ajax({
    success: success,
    error: function() {
        success();
        // any additional code
    }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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