Search code examples
javascriptajaxwebmethod

Start ajax server script and poll for completeness


I have an AJAX Request that takes a long time to complete executing. So long that it usually times out and just continues running on server. So I was thinking that if there was some way to just start it and occasionally poll the same request for completeness, that would be ideal.

So my question is can you run a server script, let it run, and poll occasionally to see if its completed? I'm using a web method to run on server if that helps/matters.

I don't want to store the data on a database so I cant just poll for the database so can I poll the script itself.

My fallback is however to create a temp table so I can call from it to see progress. Or is there some better way?


Solution

  • Firstly if you do not want your Ajax Request to timeout, you can use the 'timeout' attribute. XMLHttpRequest object used for Ajax calls has a 'timeout' attribute. Check out the documentation at http://www.w3.org/TR/XMLHttpRequest/#the-timeout-attribute .If you are making the Ajax call using jquery, you can simply do something like

    $.ajax({
      url: 'your url',
      timeout: 100000 //this sets the timeout as 100 sec
      ……//your other attributes for the call
    });
    

    If you are interested in tracking your request progress, that might be a little difficult unless you want to track the completeness(complete), error, success or a particular status code. jquery provides different(complete, error, success and status code) properties for this. If you write a native ajax call(without query), the onreadystatechange event can track the following. 0 - Unsent, 1- Opened, 2 - Response_Headers received, 3 - Loading(response is being received), 4 - Done. More at http://www.w3.org/TR/XMLHttpRequest/#event-handlers. Tracking anything besides this would be for you to develop, maintain and monitor in your code I think.