1

I am using the JQuery 1.9.1 . This question may be a possible duplicate of some previous questions.But those are out of date.So I need an upto date solution.

For my java application , I am sending a continuous request to the tomcat server for every 10 sec through JQuery-Ajax to get the data like ,

function startPolling(){

    $.ajax({
        type: "POST",
        cache: false,
        url: "My URL",
        dataType: "html",
        timeout: 10000,

        success: function(response) {                       
            if(response != null && response !="" && response !="null")
                $("#sucess").html(response);
        },
        error: function(xhr) {
            xhr.abort();
            //alert('Error: ' + e);
        },

        complete: function(xhr, status) {
            xhr.abort(); 
            startPolling();
        },
    });
}

Please make sure am I abort/cancel the client side previous request in proper manner.

Also how do I abort the server side request processing , Before sending the new request through ajax.

Hope our stack users will help me.

Human Being
  • 8,269
  • 28
  • 93
  • 136
  • Simple answer you can't do it from client side. – Arun P Johny Sep 17 '13 at 07:25
  • @Arun I have seen your previous answer http://stackoverflow.com/questions/4551175/how-to-cancel-abort-jquery-ajax-request/4551178 . But that was very old. – Human Being Sep 17 '13 at 07:27
  • one possible way is the server to return a process reference id whenever a request is made then sent the process id back to the sever with a cancel request and in server side you need to apply some kind of cancel logic – Arun P Johny Sep 17 '13 at 07:28
  • That is the problem with `abort` even though you abort the request it affects only the client side, the server side processing will continue – Arun P Johny Sep 17 '13 at 07:30
  • @Arun Am I aborting the previous client side ajax request correctly using JQuery 1.9.1 ? – Human Being Sep 17 '13 at 07:31
  • no there is a bug in the code – Arun P Johny Sep 17 '13 at 07:33
  • Try `var pollXhr; function startPolling(){ if(pollXhr){ pollXhr.abort() } pollXhr = $.ajax({ type: "POST", cache: false, url: "My URL", dataType: "html", timeout: 10000, success: function(response) { if(response != null && response !="" && response !="null") $("#sucess").html(response); }, complete: function(xhr, status) { pollXhr = null; startPolling(); }, }); }` – Arun P Johny Sep 17 '13 at 07:34
  • Is this correct `xhr.ajaxStop();` ? – Human Being Sep 17 '13 at 07:35
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/37499/discussion-between-kite-player-and-arun-p-johny) – Human Being Sep 17 '13 at 07:35

1 Answers1

1

There is a bug in the client side abort here

var pollXhr;
function startPolling(){
    if(pollXhr){
        pollXhr.abort()
    }

    pollXhr = $.ajax({
        type: "POST",
        cache: false,
        url: "My URL",
        dataType: "html",
        timeout: 10000,

        success: function(response) {                       
            if(response != null && response !="" && response !="null")
                $("#sucess").html(response);
        },
        complete: function(xhr, status) {
            pollXhr = null;
            startPolling();
        },
    });
}

Note: This will not stop the server side processing

Arun P Johny
  • 384,651
  • 66
  • 527
  • 531