Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not Waiting for Response from an AJAX Request

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...

like image 202
FinalForm Avatar asked Jul 21 '11 19:07

FinalForm


People also ask

Does AJAX wait for response?

Ajax requests do just that. With this, all codes following this one will execute immediately; you will not have to wait until the function returns. This way, your user would not have to wait for findItem() to return a result. Of course, because you still need an answer, you would have to include a callback.

What is async true in AJAX call?

by default async is true. it means process will be continuing in jQuery ajax without wait of request. Async false means it will not go to next step untill the response will come.

Is AJAX request GET or POST?

GET vs POST in AJAX callsUnless you are sending sensitive data to the server or calling scripts which are processing data on the server it is more common to use GET for AJAX calls. This is because when using XMLHttpRequest browsers implement POST as a two-step process (sending the headers first and then the data).

Is AJAX successful deprecated?

Yes, it is deprecated in jQuery 1.8 onwards. You should use . done() and use . fail() to catch the errors.


2 Answers

This may depend on your server configuration, but in general the script will continue to execute despite a closed HTTP connection.

I have tested this with Apache 2 + PHP 5 as mod_php. I would expect similar behaviour with PHP as CGI and with other webservers but do not know for certain.

The best way to determine for certain on your configuration is, as @tdammers suggests: set up a test script something like the following and monitor the log.

<?php
error_log('Test script started.');
for ($i = 1; $i < 13; $i++) {
    sleep(10);
    error_log('Test script got to ' . (10 * $i) . ' seconds.');
}
error_log('Test script got to the end.');
?>

Access this script (at /test.php or whatever) then before you get any results, hit stop on your browser. This is equivalent to navigating away before your XHR returns. You could even have it as the target of an XHR and navigate away.

Then check your error log: you should have a start and then messages every 10 seconds for two minutes and an end. You can modify how high $i gets to ensure your script will reach its anticipated maximum execution time if you'd like to test that too.

You don't have to use error_log() - you could write to a file, or make some other persistent change on the server that can be checked without needing to keep the client connection open.

The script execution time may stop before then because of the max_execution_time php.ini directive - but in any case this should be distinct from when the webserver times out.

like image 86
mjec Avatar answered Oct 20 '22 00:10

mjec


Try ignore_user_abort(true);

ignore_user_abort(true);

it should not abort proccessing of your code

like image 28
genesis Avatar answered Oct 19 '22 23:10

genesis