I\'m having problems canceling my XHR requests when navigating between pages. I have a page that has 8 requests that get fired off. I cancel them on click of a link outside
I assume that you have done that, but check all log files (php and apache).
Also try this:
php.ini
upload_max_filesize = 256M
post_max_size = 256M
.htaccess
php_value upload_max_filesize 256M
php_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);
});
Adressing either of the above two points breaks the chain and may fix the problem:
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.
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.
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 request
xhr.abort()