0

I have once ajax call which does connect to database, sets one flag in database and waits for one file to be created by some other process which looks up the flag.

Till the file is created on server, ajax keeps waiting. Once file is created, ajax reads the file and displays the content.

Now issue is that sometime the backend process which creates the file, stuck into socket issue, and never created the file, but ajax keeps waiting in that case, which gets timedout only when its timeout occurs,

What all I want to show a stop request button, which could stop current ajax request as timeout is long, and user could want to stop it prior to timeout.

Here is what I tried, I modified the code which keeps looking the existence of file, and added a session variable check , like if Session("AbortCheck") = true then exitout from the script.

and placed another ajax call to update the sessin variable so that on loop when the script will find session = true it will immediately stop the request.

While debugging I found that my my new ajax request which updates the session variable actually executed only when the first ajax is completed. and therefore its never stopped on user request to stop the request.

Could anyone let me know how to stop a running ajax (which is taking time) on user input ?

Thanks

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
NitinKumar.001
  • 179
  • 5
  • 17

2 Answers2

0

Maybe you could implement some error management in your AJAX script instead of letting the user manages that.

            'Try to open the socket here

            on error resume next

            if err<>0 then
               Response.Write("1")
            else
               Response.Write(fileContent)
            end if      

Then you get the caller script notifies the error to the user if the AJAXs script returns 1.

Julien Bourdon
  • 1,713
  • 17
  • 28
  • Hi, Thanks for response. But I have already implemented error management, like if filesize is 0kb, I let user know about it, or if something is wrong in file that I need to read. Here as my script is stuck in loop where I am checking filesystem directory, to check if file arrived or not, and as its taking time, I want to stop the request on user choice. ( User choice is required as its VMS operating system realtime graph plotting ajax ) and connection to VMESX os takes time, so my timeout is longer. Thanks. – NitinKumar.001 Oct 24 '11 at 17:09
  • What about using jQuery abort then? See this question: http://stackoverflow.com/questions/446594/kill-ajax-requests-using-javascript-using-jquery – Julien Bourdon Oct 24 '11 at 17:13
  • Hi Julien I tried this
    ajax call with jquery works but abort is not working.
    here is my code
    var xmlhttp2;
    myAjaxfunction()
    {
    xmlhttp2 = $.ajax({ 
    type: "GET", 
    url: "serverSideAjaxPage.asp", 
    data: "Function=FunctionName&nodename="+nodename, }).done(AjaxCallBackFunction);
    }
    
    And here is the function which is called on stop request click function AbortAjax() { if(xmlhttp2) { alert('aborting xmlhttp2'+xmlhttp2); xmlhttp2.abort(); alert('aborted xmlhttp2'+xmlhttp2); } } In second alert I get xmlhttp2 as object,and its not aborting. Am I missing something ?
    – NitinKumar.001 Oct 24 '11 at 18:20
  • I analyzed further, and found that request is aborted but only on client side, and the code which was running on server side [The Loop in asp code for file system object still kept running], How the server side execution could be stopped while in process ? Thanks for your help. – NitinKumar.001 Oct 24 '11 at 20:07
  • The execution of server code is still in process even after aborting the ajax jquery call , could you please suggest me if that could be triggered to stop somehow ? – NitinKumar.001 Oct 25 '11 at 20:07
  • I guess you need to do some more exception handling to detect when there is a socket error then and return this error to the client. – Julien Bourdon Oct 26 '11 at 09:28
  • I have fixed the server side execution by removing the loop from server side which continuously checks for file, and put a loop in js file to re check the same, and for handshake , if file is not found on server, I pass message as waiting to ajax and another flag for abort check from jquery, which solved the issue. Thanks for your help – NitinKumar.001 Oct 31 '11 at 16:06
0

I have fixed the server side execution by removing the loop from server side which continuously checks for file, and put a loop in js file to re check the same, and for handshake , if file is not found on server, I pass message as waiting to ajax and another flag for abort check from jquery, which solved the issue.

NitinKumar.001
  • 179
  • 5
  • 17