[英]Jquery $.ajax statusCode Else
In a jquery Ajax call I am currently handling statusCode of 200 and 304. But I also have "Error" defined" To catch any Errors that could come back.在 jquery Ajax 调用中,我目前正在处理 200 和 304 的状态码。但我也定义了“错误”以捕获可能返回的任何错误。
If there is a validation message related we return the status code of 400 - Bad Request.如果有相关的验证消息,我们将返回状态代码 400 - Bad Request。
This then falls into the "Error" function before falling into the statusCode "400" function I had defined.这然后落入“错误”function,然后落入我定义的 statusCode“400”function。 Which means two actions happen.
这意味着发生了两个动作。
Ideally I would like to not define "Error" and "Success" and only define "statusCode" But what I need is to have a "Else" so that I don't need to declare every statusCode that exists only the 2-3 I want to handle differently.理想情况下,我不想定义“错误”和“成功”,而只定义“statusCode”,但我需要的是有一个“Else”,这样我就不需要声明只存在于 2-3 I 中的每个 statusCode想以不同的方式处理。
$.ajax({
type: 'POST',
contentType: "application/json",
url: "../API/Employees.svc/" + EmployeeId + "/Company/" + CompanyId,
data: jsonString,
statusCode: {
200: function () { //Employee_Company saved now updated
hideLoading();
ShowAlertMessage(SaveSuccessful, 2000);
$('#ManageEmployee').dialog('close');
},
304: function () { //Nothing to save to Employee_Company
hideLoading();
$('#ManageEmployee').dialog('close');
if (NothingToChange_Employee) {
ShowAlertMessage(NothingToUpdate, 2000);
} else {
ShowAlertMessage(SaveSuccessful, 2000);
}
}
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
AjaxError(XMLHttpRequest, textStatus, errorThrown);
}
});
Since the "complete" event is always fired you could simply get the status code from there and ignore the success and error functions由于总是触发“完成”事件,您可以简单地从那里获取状态代码并忽略成功和错误函数
complete: function(e, xhr, settings){
if(e.status === 200){
}else if(e.status === 304){
}else{
}
}
This is what i'd use:这就是我要使用的:
error: function (xhr, textStatus, errorThrown) {
switch (xhr.status) {
case 401:
// handle unauthorized
break;
default:
AjaxError(xhr, textStatus, errorThrown);
break;
}
}
jQuery AJAX response complete
, success
, error
have been deprecated. jQuery AJAX 响应
complete
、 success
、 error
已弃用。 More up-to-date version with .done
, .fail
, .always
promise instead.使用
.done
、 .fail
、 .always
promise 代替的更新版本。
On success .always
has signature of .done
, on failure it's signature changes to that of .fail
.成功
.always
具有.done
的签名,失败时它的签名更改为.fail
的签名。 Using the textStatus
you can grab the correct variable and return the body contents.使用
textStatus
您可以获取正确的变量并返回正文内容。
var jqxhr = $.ajax( {
type: frm.attr('method'),
url: frm.attr('action'),
data: frm.serialize(),
dataType: 'json',
} )
.done(function( data, textStatus, jqXHR ) {
alert( "success" );
})
.fail(function( jqXHR, textStatus, errorThrown ) {
alert( "error" );
})
.always(function( data_jqXHR, textStatus, jqXHR_errorThrown ) {
if (textStatus === 'success') {
var jqXHR = jqXHR_errorThrown;
} else {
var jqXHR = data_jqXHR;
}
var data = jqXHR.responseJSON;
switch (jqXHR.status) {
case 200:
case 201:
case 401:
default:
console.log(data);
break;
}
});
jqxhr.always(function() {
alert( "second complete" );
});
To keep the approach similar to your initial logic, I would continue passing a statusCode object. However, you still know that "else" will fall in the realm of 4xx or 5xx type error codes.为了使方法与您的初始逻辑相似,我将继续传递 statusCode object。但是,您仍然知道“else”将属于 4xx 或 5xx 类型错误代码的 realm。
So I would update your original code to:所以我会将您的原始代码更新为:
var statusCodeResponses = {
200: function () { //Employee_Company saved now updated
hideLoading();
ShowAlertMessage(SaveSuccessful, 2000);
$('#ManageEmployee').dialog('close');
},
304: function () { //Nothing to save to Employee_Company
hideLoading();
$('#ManageEmployee').dialog('close');
if (NothingToChange_Employee) {
ShowAlertMessage(NothingToUpdate, 2000);
} else {
ShowAlertMessage(SaveSuccessful, 2000);
}
}
};
var genericElseFunction = function(response){
// do whatever other action you wanted to take
};
for(var badResponseCode=400; badResponseCode<=599; badResponseCode++){
statusCodeResponses[badResponseCode] = genericElseFunction;
}
$.ajax({
type: 'POST',
contentType: "application/json",
url: "../API/Employees.svc/" + EmployeeId + "/Company/" + CompanyId,
data: jsonString,
statusCode: statusCodeResponses,
error: function (XMLHttpRequest, textStatus, errorThrown) {
AjaxError(XMLHttpRequest, textStatus, errorThrown);
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.