0

I'm running a test site in IIS on a Windows 7 machine.

I'm sending an ajax request to the server to run, which I know is working as I'm saving a record of this in my db.

Once I start the process running I'm wanting to be able to navigate away from the page and leave the script running.

At the moment when I start the script and select a different page from my navigation my browser appears to stop the ajax request (the script is still running server side) but doesn't allow my browser to redirect until the serverside script has finished running.

I'm using:

$.post('path_to_script',{key:val},function(data){});

In my jQuery file to call the script and in my php script I'm using:

ignore_user_abort(true);
set_time_limit(0);

As I've seen suggested in other posts on here.

Am I missing something as this is my first attempt at running a background process?

2 Answers2

1

I think it's session problem. Your path_to_script script starts session, and until it closes the session, no other php script can open session. And as aborting the request does not stop the ajax script, the remedy is to close the session as soon as possible.

Marek
  • 7,337
  • 1
  • 22
  • 33
0

It is a front-end problem. Basically you have to cancel the ajax request on page unload. The php script itself will keep running in the background

var ajax_request = $.ajax(...); 
$(window).unload( function () { ajax_request.abort(); } );
lePunk
  • 523
  • 3
  • 10
  • or if this doesn't work and your data is not too large (less than 2000 chars if you are supporting IE or less than 8000 chars for proper browsers) you can try a jsonp request – lePunk Jul 17 '13 at 09:31
  • is .unload() not a depriciated jQuery function as of version 1.8 ? – timothystringer Jul 17 '13 at 09:39
  • you are right, sorry about that. not a function which I'm using too often. what about window.onunload = function() { ajax_request.abort(); } – lePunk Jul 17 '13 at 09:43
  • There is a good thread [here](http://stackoverflow.com/questions/446594/abort-ajax-requests-using-jquery) – Brewal Jul 17 '13 at 09:44