In jQuery ajax, how to handle HTTP 408 error correctly?

I am making a jQuery Ajax call with the error option set. In my “error” method, I would like to handle HTTP 408 differently from regular HTTP 500.

The problem is that jxhr.statusCode is 0, and the value of “status” is just “error”! But in firebug, I see that the service definitely returned a 408 status code.

Why is jxhr.statusCode code 0? Is there any other way to identify HTTP 408 error?

My error handler code looks like this (for error 408, the code just continues in the 'else' block):

error: function (jxhr, status, error) { var $message = element.next().find('.loadmask-msg-label'); if (jxhr.status == 401) $message.html(settings.unauthorizedMessage); else if (jxhr.status == 408) $message.html(settings.timeoutMessage); else $message.html(settings.errorMessage); 
+4
source share
2 answers

The problem was FIREFOX. In any browser other than Firefox, the status code is 408. In Firefox, the status code is 0, and response headers are not returned. Also, Firefox seems to be re-requesting 10 times due to a 408 response, I have no idea why! And this is only for HTTP 408 response.

In the end, we had to return an HTTP 500 error with custom headers.

Dirty: (

+3
source

According to jQuery you should do the following:

enter image description here

 $.ajax({ statusCode: { 404: function() { alert('page not found'); } } }) 

;

+3
source

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


All Articles