수색…


"오류"콜백으로 오류 해석

서버가 제대로 관리하면 오류는 2xx와 다른 특정 HTTP 상태 코드로 클라이언트에 반환됩니다 ( RFC 2616 섹션 10 참조).

아래 예에서 볼 수 $.ajaxSetup() 에서 오류를 세계적으로 발견하는 것이 좋습니다. 따라서 아약스 호출에서 오는 모든 오류는 아약스 설정에서 자동으로 해석됩니다.

$.ajaxSetup({
    error: function (jqXHR, exception, errorThrown) {
        var message;
        var statusErrorMap = {
            '400': "Server understood the request, but request content was invalid.",
            '401': "Unauthorized access.",
            '403': "Forbidden resource can't be accessed.",
            '500': "Internal server error.",
            '503': "Service unavailable."
        };
        if (jqXHR.status) {
            message = statusErrorMap[jqXHR.status];
            if (!message) {
                message = "Unknown Error.";
            }
        } else if (exception == 'parsererror') {
            message = "Error.\nParsing JSON Request failed.";
        } else if (exception == 'timeout') {
            message = "Request Time out.";
        } else if (exception == 'abort') {
            message = "Request was aborted by the server";
        } else {
            message = "Unknown Error.";
        }

        // How you will display your error message...
        console.log(message);
        console.log(errorThrown);
    }
});

특정 오류 메시지를 기다리고있을 때 error 콜백을 특정 $.ajax() 로 "오버로드"할 수도 있습니다.

$.ajax({
    url: './api',
    data: { parametersObject },
    type:'post',
    dataType: 'json',
    success:function(output){
        // Interpret success
    },
    error: function(xhr,textStatus,ErrorThrown){
        // Specific error will not be interpreted by $.ajaxSetup
    }
});


Modified text is an extract of the original Stack Overflow Documentation
아래 라이선스 CC BY-SA 3.0
와 제휴하지 않음 Stack Overflow