Alex Rivera | Logout

Not Waiting for Response from an AJAX Request

Asked 2011-07-21T19:54:43.003
9

Suppose I make an AJAX HTTP Request from jQuery to a backend PHP script. The request is made, the PHP script starts running and doing its magic. Suppose I then change to another website, away from the site where the original AJAX Request was made. As well, I do this before the PHP script finishes and has time to do a HTTP Response back. Does the PHP script finish running and doing its thing even though I've switched to another website before I got the HTTP Response?

So the order is this.

  • I'm on website www.xyz.com
  • I have a jQuery handler that kicks off an AJAX request to blah.php
  • blah.php starts running
  • I go to website www.abc.com soon after without waiting for a response from blah.php

What's going on with blah.php? Is execution still going on? Did it stop? I mean it didn't get a chance to respond so...

Edit
Report

1 Answer

0

You might want to check out the answers to This Question. Basically when you make your ajax call to a php function which calls the exec() function as shown in the answers to that question, you'll get an ajax response almost immediately, since your php function doesn't actually need to process anything. This way, it shouldn't matter if the user leaves the page. Here's a small example:

ajax call in html file: $.ajax({url: 'blah.php'});

blah.php file: exec('bash -c "exec nohup setsid php really_slow_script.php > /dev/null 2>&1 &"');

And then finally in really_slow_script.php, just include the actual code you want to run.

I successfully used this kind of logic to allow users to post an already uploaded video from their account on my website to youtube. (The video had to be sent to youtube, and since videos are generally large files, I didn't want the user to have to wait while the video was being uploaded to youtube)

answered 2011-08-27T04:28:34.223

Your Answer