JQuery Ajax aborts and new request in quick succession

I have a mobile application that makes

JqXHR = $.ajax({ url: 'url', data: null, async: true, cache: false, dataType: 'json', type: 'GET', crossDomain: true, timeout: 63000, success: function(data, textStatus, jqXHR) {}, error: function(jqXHR, textStatus, errorThrown) {} }); 

request. It waits for 63 seconds (so that user interaction at the other end is done through the PHP backend for ~ 62 seconds). Now, if at the same time I decide to cancel this request, I call JqXHR.abort() . In the error handler, I already process / distinguish between real errors and interrupts, which works. Immediately after the cancellation, I want to send another API call to the server to tie the loose ends and make sure my cancellation request is registered.

And there is a problem. Even if I abort() first request, the PHP script is still running on the server, which will not be a problem if it also executes the second request, which will cause it to stop and die (). But this does not happen. The second request is not executed until the first completion.

Any ideas?

jQuery 1.8.2, jQuery Mobile 1.2.0, PhoneGap 2.0.0 and 2.1.0, Apache 2, Linux, PHP 5.3

+7
source share
3 answers

I seem to have come across good old PHP and AJAX sessions. In fact, my boss found out about this issue, resorting to some expressions that I never thought of. I use the Zend framework in the background, and it automatically starts the session namespace, so in my API method before the Dispatch () of the controller, I had to add @session_write_close (); and, as if by magic, it works like a charm.

Thanks to Arun for your quick reply, it is most common.

So, in short: if you use Zend Framework or session_autostart or other ways to start sessions, they will not fly with concurrent AJAX requests.

+1
source

Some info: Parallel-Ajax vs. Apache-Session blocking


Session data is usually saved after your script is completed , but since session data is locked to prevent simultaneous writing, one script can run in a session at any time .

When, for example, using sets of frames along with sessions, you will observe the loading of frames one at a time because of this lock. You can reduce the time required to download all frames by ending the session as soon as possible .


This way you can use sessions in ajax scripts using session_start(); (possibly handled automatically), followed by (as soon as possible) session_write_close();

session_write_close(); ends the current session and saves the session data.

But: session_id() will still provide the correct (current) PHPSESSID so that you can re-access the current session by simply doing session_start() again anytime you need it.


I use it this way in all my ajax scripts to implement session processing and provide concurrent request

+3
source

The abort method will not terminate the server process, it will simply end up waiting for a server response on the client side.

0
source

All Articles