11

I have a WCF service which takes a long time to process the first time it is called, and then caches those results in HttpRuntime.Cache. In order to initialize this cache I'd like to trigger a fire-and-forget ajax call from javascript.

Right now I have this javascript in the page:

$.ajax({
    type: 'GET',
    url: getServiceURL() + 'PrimeCacheAjax',
    contentType: 'application/json; charset=utf-8'
});

Where the PrimeCacheAjax function just performs a dummy call to populate the cache.

The only issue with this approach is that the page with this ajax call is a type of landing page which executes some javascript, opens another window and closes itself. When the window closes itself before the server responds, I see a cancelled request in fiddler. I am concerned that this may lead to situations where the ajax call may not reach the server, is this possible?

Is there a way to specify (using $.ajax()) that no response will be coming, or does it not really matter?

jbabey
  • 45,965
  • 12
  • 71
  • 94

2 Answers2

7

Only time it will matter is if the request to the server is not complete. You should check to make sure the call is at a readyState value of 2 [aka sent], before exiting.

epascarello
  • 204,599
  • 20
  • 195
  • 236
  • Waiting for `readyState==2` means waiting for the response headers from the server, which is *slightly* longer than the OP wants to wait, but I think this answer is the best possible solution available using the XHR API as it exists. – apsillers Dec 11 '12 at 19:55
  • Any idea how ajax requests react to the server flushing the buffer? I've used that with an iframe before to get similar results where i flushed a – Kevin B Dec 11 '12 at 19:57
  • waiting for the readystate would probably be too long, if the user sees the launch page at all they will cry "DEFECT!". I guess we will just hope the request goes out - worst case scenario they wait a little longer for the cache to populate on their first hit. – jbabey Dec 11 '12 at 20:30
0

I would just perform a call with no callback, I don't believe there is a property that allows the F&F method.

for a very short ajax call you could try the following code as an alternative, if you wanted.

$.get('URL');
Dennis Martinez
  • 6,344
  • 11
  • 50
  • 67