Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Ajax abort and new request in quick succession

I have a mobile app, which makes a

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 (the PHP backend CAN run for ~62 seconds) for user interaction at the other end. Now, if in the meanwhile I decide to abort this request, then i call JqXHR.abort(). In the error handler I already handle/differentiate between real errors and aborts, that works. Right after the abort, I want to send an other API call to the server to tie the loose ends and make sure my cancel request is logged.

And there is the problem. Even tho I abort() the first request, the PHP script is still running on the server, which wouldn't be a problem if it also executed the second request, which would make it to stop and die(). But it is not happening. The second request is not happening until the first finishes.

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

like image 231
user1741224 Avatar asked Oct 12 '12 13:10

user1741224


2 Answers

Some information to: Parallel-Ajax vs Apache-Session locking


Session data is usually stored after your script terminated, but as session data is locked to prevent concurrent writes only one script may operate on a session at any time.

When e.g. using framesets together with sessions you will experience the frames loading one by one due to this locking. You can reduce the time needed to load all the frames by ending the session as soon as possible.


So you can use sessions in ajax scripts with session_start(); (maybe handled automatically) followed immediately (soon as possible) by session_write_close();

session_write_close(); will "end" the current session and store the session data.

But: session_id() will still deliver the correct (current) PHPSESSID so you're able to re obtain write access to the current session by simply doing session_start() again at any time you need it.


I use it this way in all my ajax scripts to implement session handling and allowing parallel request

like image 50
DerDu Avatar answered Sep 30 '22 06:09

DerDu


It seems I ran into the good old PHP sessions vs. AJAX requests issue. Actually my boss found out about this issue by googling some expressions i never thought of. I am using Zend framework in the back-end, and it automatically starts a session namespace, so in my API controller's preDispatch() method I had to put in a @session_write_close(); line, and as if by magic, it works like a charm.

Thanks Arun for your quick reply, it is most appretiated.

So, in short: If you use Zend Framework or session_autostart or other means of starting sessions, they won't fly with parallel AJAX requests.

like image 42
user1741224 Avatar answered Sep 30 '22 06:09

user1741224