JQuery AJAX callback error is not called when an error occurs

I am currently trying to implement HTTP 413 processing: request the essence of too big error from my server. I have done the following:

$.ajax({
    url: "submit.php",
    data: {
        "data": POSTData
    },
    success: function(response, statusText, XHR) {
        console.log(XHR.status + ": " + response);
        resolve(); // resolve the promise and continue on with execution
    },
    // Added this part:
    error: function(response, statusText, XHR) {
        if(XHR.status === 413) {
            // Request entity too large
            // To solve this we split the data we want to upload into several smaller partitions
            // and upload them sequentially

            console.log("Error 413: Request entity too large. Splitting the data into partitions...");

            // handling code below

            // blahblahblah
        }
    },
    method: "POST"
});

But instead of triggering an error callback, my console still throws an error (it says it like 413), as if there was no handler. How to implement this functionality?

+4
source share
2 answers

You have an error callback method signature. See http://api.jquery.com/jquery.ajax/

Correct signature according to these documents: Function (jqXHR jqXHR, String textStatus, String errorThrown)

XHR.status , , XHR, .

:

 error: function (jqXHR, textStatus, errorThrown) {
    if(jqXHR.status === 413) {
        // Request entity too large
        // To solve this we split the data we want to upload into several smaller partitions
        // and upload them sequentially

        console.log("Error 413: Request entity too large. Splitting the data into partitions...");

        // handling code below

        // blahblahblah
    }
},

, , if, .

+3

jQuery.ajaxSetup() .. , , HTTP 413.

:

$(function() {
    $.ajaxSetup({
        error: function(jqXHR, exception) {
            if (jqXHR.status === 0) {
                console.log('Not connect.n Verify Network.');
            } else if (jqXHR.status == 404) {
                console.log('Requested page not found. [404]');
            } else if (jqXHR.status == 413) {
                console.log('Error [413]: Request entity too large. Splitting the data into partitions...');
            } else if (jqXHR.status == 500) {
                console.log('Internal Server Error [500].');
            } else if (exception === 'parsererror') {
                console.log('Requested JSON parse failed.');
            } else if (exception === 'timeout') {
                console.log('Time out error.');
            } else if (exception === 'abort') {
                console.log('Ajax request aborted.');
            } else {
                console.log('Uncaught Error.n' + jqXHR.responseText);
            }
        }
    });
});
+1

Source: https://habr.com/ru/post/1653329/


All Articles