Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-03-2007, 10:10 PM   PM User | #1
otnj2ee
Regular Coder

 
Join Date: Apr 2007
Posts: 174
Thanks: 17
Thanked 0 Times in 0 Posts
otnj2ee is an unknown quantity at this point
How to cancel Ajax call

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?

Thanks

Scott
otnj2ee is offline   Reply With Quote
Old 10-03-2007, 10:16 PM   PM User | #2
eak
Regular Coder

 
eak's Avatar
 
Join Date: Jun 2002
Location: Nashville, TN
Posts: 354
Thanks: 0
Thanked 26 Times in 26 Posts
eak is on a distinguished road
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?
__________________
eak | "Doing a good deed is like wetting your pants; every one can see the results, but only you can feel the warmth."
eak is offline   Reply With Quote
Old 10-17-2007, 03:27 AM   PM User | #3
glenngv
Supreme Master coder!


 
glenngv's Avatar
 
Join Date: Jun 2002
Location: Los Angeles, CA Original Location: Philippines
Posts: 10,241
Thanks: 0
Thanked 112 Times in 111 Posts
glenngv will become famous soon enough
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.
__________________
Glenn
_____________________________________________
Play Tower of Hanoi Android app (Ad-FREE!)
Play Tower of Hanoi Android app (FREE!)
Go to Tower of Hanoi Leaderboard
Play Tower of Hanoi Facebook app
glenngv is offline   Reply With Quote
Old 04-10-2008, 09:56 PM   PM User | #4
Daya Kumar
New to the CF scene

 
Join Date: Apr 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Daya Kumar is an unknown quantity at this point
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.
Daya Kumar is offline   Reply With Quote
Old 07-02-2008, 10:54 PM   PM User | #5
BrightNail
Regular Coder

 
Join Date: Jun 2002
Posts: 349
Thanks: 2
Thanked 0 Times in 0 Posts
BrightNail is an unknown quantity at this point
hmmm.

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.
BrightNail is offline   Reply With Quote
Old 09-10-2009, 11:54 PM   PM User | #6
rgblkcal
New to the CF scene

 
Join Date: Sep 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
rgblkcal is an unknown quantity at this point
Lightbulb Did this using DWR (directwebremoting)

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">
dwr.engine.setActiveReverseAjax(true);
PushMsg.initMsg();


function cancelAction() {
PushMsg.cancel();
}

</script>

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:
http://directwebremoting.org/dwr/index.html
rgblkcal is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:32 PM.


Advertisement
Log in to turn off these ads.