在一个jQuery的Ajax调用中,我目前处理的状态码为200和304。但是我也定义了"Error"以捕获可能返回的任何错误。
如果有相关的验证消息,则返回状态码400 - 错误请求。
这会先进入"Error"函数,然后再进入我定义的statusCode "400"函数。这意味着会发生两个动作。
理想情况下,我不想定义"Error"和"Success",只定义"statusCode"。但我需要一个"Else",这样我就不需要声明每个存在的statusCode,只需处理2-3个不同的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);
}
});