Catch "0" errors in jQuery Ajax

Viewed 26

Using this code, I'm sending a request to the server, and in the error section checking to see if the request was successful, so I can display an "offline" message in the html if the client isn't connected to the internet:

$.get({
    url: '/files',
    success: update,
    error: requestError
});

function update(data)
{
    // do stuff with the data
}

function requestError(xhr)
{
    if(xhr.readyState == 0)
    {
        $('#files, #upload').css('display', 'none');
        $('#offline').css('display', 'inline-block');
    }
    else
        console.error(xhr.status);
}

This works as intended: when the request is successful, it runs the update() function, and when I turn off the internet for the client, it displays the proper error message in the html. However, it also throws a net::ERR_INTERNET_DISCONNECTED error in the console, even though I've handled it already. This doesn't affect the function of the program but does fill up the console. Is there any way to prevent this error from showing up? According to this answer, only >=400 errors are caught, but it doesn't actually explain how to catch the other errors.

0 Answers
Related