Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I immediately start a new ajax request after aborting for the previous one?

Tags:

jquery

ajax

I have a web application where the HTTPs handshake take place for every connection and I'm determined to find out why. The HTTP keep-alive is working, so it's something else that forces a request to start a new HTTPs handshake when it shouldn't.

I have something like this (pseudocode):

   startNewRequest() {
     if (oldRequest)
        oldRequest.abort(); // abort old request
     oldRequest = $.ajax(); // start new request
   }

The request object is XMLHttpRequest returned by jQuery's ajax method. Everytime when I start a request, I abort for the previous request.

My questions is that whether this would force a new request to start a HTTP handshake because the old one hasn't been completed. The requests are regular POST. Do I need to explicit wait for about() to complete before starting off a new request so to take advantage of the HTTP keep alive?

I do get a call in the error callback for $.ajax() for every request I abort. Do I need to start a new request only after I get a callback in the error handler?

like image 745
SmallChess Avatar asked Apr 07 '14 08:04

SmallChess


1 Answers

Use complete or error in $.ajax

Complete:

$.ajax({
   url : RequestUrl,
   complete : function(){
       // Do something when request complete
   }
});

Error:

$.ajax({
   url : RequestUrl,
   error: function(){
       // Do something when an error occurred when the request is done
   }
});
like image 78
Andreas Furster Avatar answered Sep 30 '22 02:09

Andreas Furster