Jquery $.ajax statusCode Else

陌路散爱 提交于 2019-11-30 10:56:58

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. More up-to-date version with .done, .fail, .always promise instead.

On success .always has signature of .done, on failure it's signature changes to that of .fail. Using the textStatus you can grab the correct variable and return the body contents.

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.

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);
    }
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!