Timeout not working in ajax post request

Fix :

Change async : false to async: true

Reason :

A synchronous AJAX call blocks until the request has been finished. Implementing a timeout is not possible for technical reasons, because the AJAX call would have to be executed later.

If an AJAX call is executed later, the function somehow has to implement a blocking feature, to stop the code from running further after the AJAX call, and execute it again after a timeout - not possible.


Today, (in 2019) I still have this problem.

I have an ajax call too long (depending from Date Start-> Date End) php script.

and after some minutes I get error 500, async: true don't help.

The call is:

$.ajax({
    type: "GET",
    dataType: 'json',
    url: 'mymscript.php',
    contentType:'application/json;charset=UTF-8;',
    data: $('[name="myForm"]').serialize(),
    async:true,
    timeout:0,
success: function(response){
...

I resolved using: side PHP:

set_time_limit(0);
ignore_user_abort(true);

at begin of script.

echo ' ';
flush();
ob_flush();

in the middle of script (for example in main loop every day). This help to let client to don't disconnect (I think that is the main problem).

Using this I continuosly write spaces befor the final json. Fortunately jquery trim spaces before to parse the json and, in the end, the json is valid.

So I can catch response object to know if script is ended with errors or warnings.

I Hope this help somebody.