Why does 500 errors interrupt ajax call?

This is my code:

http_request = new XMLHttpRequest(); if (http_request.overrideMimeType) http_request.overrideMimeType('text/javascript'); http_request.onreadystatechange = alertContents; http_request.open('POST', base_url, true); http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); http_request.setRequestHeader("Content-length", parameters.length); http_request.setRequestHeader("Connection", "close"); http_request.send(parameters); 

Running in the latest firefox.

This code works fine, but when the called page causes an internal internal server error, javascript stops on the page, and further events scheduled with setInterval / setInterval are not executed. Why is this happening? How to catch him?

+4
source share
1 answer

The problem may be in your alertContents function (not shown in your question). A function called .onreadystatechange almost always looks something like this:

 var callback = function() { if (http_request.readyState==4 && http_request.status==200) { // do stuff } } 

if the page creates 500 Http code, then the if statement will fail, and the rest of the code in the callback will not be executed. To catch this, add:

 else if(http_request.readyState==4 && http_request.status==500) { console.log('The server\ on fire!'); } 
0
source

All Articles