取消之前正在运行的Ajax请求

13

之前已经发送的Ajax请求可以取消吗?

var xhr = $.ajax({
    type: "POST",
    url: "some.php",
    data: "name=John&location=Boston",
    success: function(msg){
       alert( "Data Saved: " + msg );
    }
});

那么 if(xhr){xhr.abort();} 怎么样? - Drixson Oseña
我认为这是可能的,它在 ASP.NET AJAX 工具包中可用。 - Devesh
感谢所有的回答。现在问题已经解决了。 - Dileep Kheni
1个回答

7

尝试类似以下的内容

        $(document).ready(function(){
            var xhr; // global object

            function your_function() {
                if(xhr && xhr.readystate != 4){
                    xhr.abort();
                }

                xhr = $.ajax({
                    type: "POST",
                    url: "some.php",
                    data: "name=John&location=Boston",
                    success: function(msg){
                       alert( "Data Saved: " + msg );
                    }
                });
            }
        });

谢谢,它对我有效。 - Dileep Kheni

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接