Cancel pending AJAX requests in PHP app? Cancel pending AJAX requests in PHP app? ajax ajax

Cancel pending AJAX requests in PHP app?


Probable causal chain

  1. the server does not realise the XHR requests are cancelled, and so the corresponding PHP processes keep running
  2. these PHP processes use sessions, and prevent concurrent access to this session until they terminate

Possible solutions

Adressing either of the above two points breaks the chain and may fix the problem:

  1. (a) ignore_user_abort is FALSE by default, but you could be using a non-standard setting. Change this setting back to FALSE in you php.ini or call ignore_user_abort(false) in the scripts that handle these interruptible requests.

Drawback: the script just terminates. Any work in progress is dropped, possibly leaving the system in a dirty state.

  1. (b) By default, PHP will not detect that the user has aborted the connection until an attempt is made to send information to the client. Do echo something periodically during the course of your long-running script.

Drawback: this dummy data might corrupt the normal output of your script. And here too, the script may leave the system in a dirty state.

  1. A PHP sessions is stored as a file on the server. On session_start(), the script opens the session file in write mode, effectively acquiring an exclusive lock on it. Subsequent requests that use the same session are put on hold until the lock is released. This happens when the script terminates, unless you close the session explicitely. Call session_write_close() or session_abort() as early as possible.

Drawback: when closed, the session cannot be written anymore (unless you reopen the session, but this is somewhat inelegant a hack). Also the script does keep running, possibly wasting resources.

I definitely recommend the last option.


Are you storing your Ajax Request on a variable?. If not, that's what you need to do to completely cancel a request

var xhr = $.ajax({    type: "POST",    url: "anyScript.php",    data: "data1=0&data2=1",    success: function(msg){       //Success Function    }});//here you abort the requestxhr.abort()


I assume that you have done that, but check all log files (php and apache).

Also try this:

php.ini

upload_max_filesize = 256Mpost_max_size = 256M

.htaccess

php_value upload_max_filesize 256Mphp_value post_max_size 256M

Another thing that bugs me is this part.

$(xm.requests).each(function () {    var t = this;    t.abort();});$(xm.intervals).each(function () {    var t = this;    clearInterval(t);});

Try passing arguments to the callback and abort through them. I have seen cases, where assigning this to a variable withing $.each loop actually points to a different object or the global window.

$(xm.requests).each(function (index, value) {        value.abort();    });    $(xm.intervals).each(function (index, value) {        clearInterval(value);    });