简体   繁体   中英

How to kill the previous ajax request in server side?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM