jQuery的$就状态code否则状态、jQuery、code

2023-09-10 16:10:44 作者:我是你无奈的选择

在一个jquery Ajax调用我目前正在处理状态$ C $ 200和304 C,但我也有错误定义,将用能回来的任何错误。

如果有相关的,我们返回400状态code验证消息 - 错误的请求。

vscode的C 开发环境配置 win10下Linux子系统

这则落入状态code400我已经定义的函数之前落入错误功能。这意味着两个动作发生。

在理想情况下,我想没有定义错误和成功,只有界定状态code但我需要的是有一个其他,这样我就不需要声明每个状态code表示只存在2-3我要处理的方式不同。

  $。阿贾克斯({
        键入:POST,
        的contentType:应用/ JSON
        网址:../API/Employees.svc/+雇员+/公司/+ CompanyId,
        数据:jsonString,
        状态code:{
            200:函数(){// Employee_Company现在保存的更新

                hideLoading();
                ShowAlertMessage(SaveSuccessful,2000年);
                $('#ManageEmployee)对话框(亲密);

            },
            304:函数(){//没有保存到Employee_Company

                hideLoading();
                $('#ManageEmployee)对话框(亲密);

                如果(NothingToChange_Employee){
                    ShowAlertMessage(NothingToUpdate,2000年);
                } 其他 {
                    ShowAlertMessage(SaveSuccessful,2000年);
                }
            }
        },
        错误:函数(XMLHtt prequest,textStatus,errorThrown){
            AjaxError(XMLHtt prequest,textStatus,errorThrown);
        }
    });
 

解决方案

由于完整事件总是解雇你可以简单地获取状态code从那里,而忽略了成功和错误功能

 完成:功能(即XHR,设置){
    如果(e.status === 200){

    }否则,如果(e.status === 304){

    }其他{

    }
}
 

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.

If there is a validation message related we return the status code of 400 - Bad Request.

This then falls into the "Error" function before falling into the statusCode "400" function I had defined. 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.

$.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{

    }
}