在jquery Ajax调用中,我当前正在处理statusCode分别为200和304。但是我还定义了“错误”,以捕获可能返回的任何错误。
如果有相关的验证消息,我们将返回状态码400-错误的请求。
然后,在属于我定义的statusCode“ 400”函数之前,属于“错误”函数。这意味着有两个动作发生。
理想情况下,我不想定义“错误”和“成功”,而仅定义“ 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);
}
});