Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Apr 2007
    Posts
    179
    Thanks
    17
    Thanked 0 Times in 0 Posts

    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

  • #2
    eak
    eak is offline
    Regular Coder eak's Avatar
    Join Date
    Jun 2002
    Location
    Nashville, TN
    Posts
    354
    Thanks
    0
    Thanked 26 Times in 26 Posts
    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."

  • #3
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,030
    Thanks
    0
    Thanked 247 Times in 244 Posts
    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.

  • #4
    New to the CF scene
    Join Date
    Apr 2008
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #5
    Regular Coder
    Join Date
    Jun 2002
    Posts
    349
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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.

  • #6
    New to the CF scene
    Join Date
    Sep 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •