View Full Version : How to cancel Ajax call

10-03-2007, 10:10 PM
There are 2 buttons on a JSP page: One is called Search, another is called Cancel.

When the Search button is pressed, it will trigger an AJAX call, this ajax will call a Java function, while this java function makes a function call to the Oracle database (store procedure/function).

This searching process will take a while... At the mean time, the user wants to cancel the searching. So the user pressed the Cancel button.

Here, I want this cancel process to cancel the Ajax process;

How can I do that?



10-03-2007, 10:16 PM
The abort method may be what you need. http://www.google.com/search?q=ajax+abort

Does your server side script continue processing if the client is disconnected?

10-17-2007, 03:27 AM
In conjunction with using abort, you can send another http request when canceling the request. This will set a session variable flag indicating the request is canceled. This flag should be checked in the server-side before performing extensive function calls. If canceled, the function call will not be performed.

Daya Kumar
04-10-2008, 09:56 PM
Hi ,

Aborting a Ajax request ' sent ' already underway may not be that trivial .
Even you if you set a Session flag indicating that the action was cancelled later on. This Session information may not be available to the delegate call that is handling the Ajax request . As this thread doesnt even know what a Session is !!. Bcoz , HttpContext.Current would be null in that thread ( if using delegate ).

You need to figure out a way your executing server code can access a variable ( flag ) and abandon process based on that flag.

Just wanted to give you a heads up.

07-02-2008, 10:54 PM

anybody find a solution to this?

Sometimes my proxy takes a long while, if it does - I want to abort the ajax and give a message ie.." Your request could not be completed at this time. Proxy server seems to be down."

etc.. Now, the proxy "might not be down", but just running VERY SLOW -- doens't matter. Either way, I want to kill the ajax call after a set time.

09-10-2009, 11:54 PM
I was able to add canceling an action by using DWR. DWR is usually used to send messages from a server to a client, but you can also use it to send a message from a client to a server.

My client side jsp for the DWR setup is as follows:

<%@ taglib prefix="s" uri="/struts-tags" %>

<%-- These files are created in the runtime --%>
<script type='text/javascript' src='/rms/dwr/util.js'></script>
<script type='text/javascript' src='/rms/dwr/interface/PushMsg.js'></script>
<script type='text/javascript' src='/rms/dwr/engine.js'></script>

<div style="color:red" id="<%= session.getId()%>"></div>
<script type="text/javascript">

function cancelAction() {


On the server side the PushMsg class contains the cancel method and another method the check if cancel was pressed as follows:

public void cancel(){
servletContext.setAttribute("cancel", true);

public Boolean isCanceled(){
return (Boolean) servletContext.getAttribute("cancel");

The onclick event on my cancel button calls the cancelAction() function which in turn calls the cancel method of the PushMsg class on the server.
I just poll the PushMsg class on the server by calling the iscanceled() method.

You can find information and examples on using DWR at:

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum