[英]jQuery Ajax success callback on failure
I'm making an ajax post request with success, failure and status code handlers. 我正在用成功,失败和状态代码处理程序发出ajax发布请求。 However, when the request fails with a 400 error, part of the success function is still called.
但是,当请求因400错误而失败时,仍然会调用成功函数的一部分。 Could someone please tell me why?
有人可以告诉我为什么吗?
$.ajax({
type: "POST",
url: saveToGetTalentUrl.url,
data: JSON.stringify(candidateList),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
$("#save_to_getTalent").replaceWith("Saved to getTalent");
alert("New User " + response.candidate.full_name + " created successfully");
console.log(msg);
//normalExec(response, msg);
},
error: function (errormessage) {
console.log(errormessage);
},
statusCode: {
400:function(){
alert(errors.err400);
},
401:function(){
alert(errors.err401);
},
403:function(){
alert(errors.err403);
},
500:function(){
alert(errors.err500);
}
}
});
}
When the call fails with a 400 error, the error from statusCode is displayed and the error is logged from the error function, but at the same time, the line $("#save_to_getTalent").replaceWith("Saved to getTalent");
当调用失败并显示400错误时,将显示statusCode中的错误,并从错误函数中记录该错误,但同时,行
$("#save_to_getTalent").replaceWith("Saved to getTalent");
is also called. 也称为。 I dont understand why
我不明白为什么
Try set async: true
to see what happens. 尝试设置
async: true
以查看会发生什么。
See this part in the jQuery documentation for ajax
: 请参阅jQuery文档中有关
ajax
这一部分:
async (default: true)
异步(默认值:true)
Type: Boolean
类型:布尔
By default, all requests are sent asynchronously (ie this is set to true by default).
默认情况下,所有请求都是异步发送的(即默认情况下设置为true)。 If you need synchronous requests, set this option to false.
如果需要同步请求,请将此选项设置为false。 Cross-domain requests and dataType: "jsonp" requests do not support synchronous operation.
跨域请求和dataType:“ jsonp”请求不支持同步操作。 Note that synchronous requests may temporarily lock the browser, disabling any actions while the request is active.
请注意,同步请求可能会暂时锁定浏览器,从而在请求处于活动状态时禁用任何操作。 As of jQuery 1.8, the use of async: false with jqXHR ($.Deferred) is deprecated;
从jQuery 1.8开始,不建议使用async:false和jqXHR($ .Deferred); you must use the success/error/complete callback options instead of the corresponding methods of the jqXHR object such as jqXHR.done().
您必须使用成功/错误/完成回调选项,而不要使用jqXHR对象的相应方法,例如jqXHR.done()。
So, the request by default ( async: true
) are sent one by one, not all together. 因此,默认情况下,请求(
async: true
)是一一发送的,而不是一起发送的。 If the POST takes a while, you may see the callback function executed before the POST request returned completely. 如果POST需要一段时间,您可能会看到在POST请求完全返回之前执行了回调函数。 When the first line of
success
is executed, it is blocked because you have a dialog popped up; 当执行
success
的第一行时,它会被阻止,因为您会弹出一个对话框。 meanwhile, the statusCode
part is also executed; 同时,
statusCode
部分也被执行; it continues to the end because nothing is blocking there. 它一直持续到最后,因为那里什么都没有阻塞。
Debug with a dialog is not the best solution because it always blocks; 使用对话框进行调试并不是最佳解决方案,因为它总是会阻塞。 you can try to change some element's value to see the execute order, like changing an
<input>
's value, append a number to see if the whole number string changes or not, to see if there is a race condition of success
and statusCode
. 您可以尝试更改某些元素的值以查看执行顺序,例如更改
<input>
的值,附加数字以查看整个数字字符串是否更改,以查看是否存在success
和statusCode
的竞争条件。
Something like: 就像是:
$.ajax({
data:
url:
success: function(returned, status, xhr) {
$('#testInput').val($('#testInput').val() + "1");
},
error: function (errormessage) {
$('#testInput').val($('#testInput').val() + "2");
},
statusCode: {
400:function(){
$('#testInput').val($('#testInput').val() + "3");
},
401:function(){
$('#testInput').val($('#testInput').val() + "4");
},
403:function(){
$('#testInput').val($('#testInput').val() + "5");
},
500:function(){
$('#testInput').val($('#testInput').val() + "6");
}
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.