简体   繁体   English

如何在服务器端终止先前的ajax请求?

[英]How to kill the previous ajax request in server side?

I am using the JQuery 1.9.1 . 我正在使用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 , 对于我的Java应用程序,我每10 sec通过JQuery-Ajaxtomcat server发送一个连续请求,以获取类似的数据,

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. 请确保我以适当的方式abort/cancelclient side先前请求。

Also how do I abort the server side request processing , Before sending the new request through ajax. 另外,如何通过ajax发送新请求之前中止server side request processing

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 注意:这不会停止服务器端处理

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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