views:

2436

answers:

6

I was wondering if there's a simple way to cancel an AJAX request?

Beyond just calling an 'abort' on the XMLHTTPRequest on the client side, is there a way to easily stop the server process? The server is using Apache.

Thanks

+3  A: 

No. There's definitely no simple way.

I can think of some complex ways, but they would be unreliable.

You'll probably have more luck developing a process for reversing the changes from a request you've just run (I'm assuming that's the reason you would want to stop it.)

Steven Richards
the reason would be to stop a long process from taking up resources so not really to do with modifications to a DB. thx for the reply
AndreLiem
As zombat mentions below, if it's a long process, you could have time to do something, but identifying the Apache thread would be very difficult. You'd have to set up some kind of storage mechanism to link running scripts to requests (which means requests need to include a unique id of some kind). If you stored that linkage in a database, you could send a second request to flag the request as cancelled, and then periodically poll that database record from your running script to see if you can stop working. It would be very convoluted and there's a lot of potential for error/malicious activity.
Steven Richards
A: 

The easiest way would be just to send a 503 (or similar) HTTP Response header and abort the server-side application (or just abort the application). Assuming no data was sent and/or you checked the HTTP response header client side you can just treat it as a failed request.

tj111
A: 

The short answer is "no". Unless you're talking about a very long running backgrounded process on the server side, in which case you could have a second Ajax request initiate a stop on it. But if you're talking about a regular request to an Apache/PHP server, then no... identifying which Apache thread your request was running in would be trouble enough.

It would be much easier to verify your Ajax request somehow before even starting the process; make sure it's what you want to be running. You could have a Javascript confirmation process as well if you need the user to be aware that what they are doing cannot be interrupted.

As Steven posted, having a reversal process sounds like it might be in your best interest as well.

zombat
+3  A: 

Initiate another Ajax request. I found this thread by trying to figure out how to prevent a previous request from being interrupted when another is called.

dan
A: 

The short answer is "no". Unless you're talking about a very long running backgrounded process on the server side, in which case you could have a second Ajax request initiate a stop on it.

How could this be done?

I used abort(); but this only prevents the result from showing in the browser but will not stop the request from running on the server.

How could it be stopped with the second Ajax request?

Thanks!

Peter Kroon
A: 

there are no good ways to stop the request itself over the network (as many have said), but it's relatively easy to stop the effects of the request. i.e.

on the server-side: you could exit (or whatever term you'd require to kill the script) by constantly checking global variables (acting as flags) like cookies or sessions (again, assuming these would change during the execution of the script). additionally, you could keep files or database entries as flags (that you can poll for existence).

on the client-side: you could call abort (which doesn't work in all browsers, like others have said), or you could keep a global semaphore / flag at the top of your callback (that checks the global value to see if it should still execute or not). example:

 window.ajaxIsStillGood = true;

 xhr.onreadystatechange = function( )
 {
     if( 4 === xhr.readyState && 200 === xhr.status )
     {
         if( true === window.ajaxIsStillGood ){ /* still good */ }
     }
 }

 /* bad stuff happens */

 window.ajaxIsStillGood = false; // now the code won't execute
Dan Beam