简体   繁体   English

如何停止通过$ .get发送的jquery ajax请求?

[英]how to stop jquery ajax request sent via $.get?

i am making a ajax request using $.get as soon as the user types a character in the input box i want to abort the previous ajax calls and then make the new one, is there a way? 一旦用户在输入框中键入一个字符,我想使用$ .get制作一个ajax请求我想中止之前的ajax调用,然后创建新的ajax,是否有办法?

edit 编辑

solved! 解决了!

i just found out that $.get is a shorthand way to $.ajax and hence it returns a XHR and so we can call abort on that variable. 我刚刚发现$ .get是$ .ajax的简写方式,因此它返回一个XHR,因此我们可以在该变量上调用abort。

You can use .abort() on XMLHttpRequest that $.get returns. 您可以在$.get返回的XMLHttpRequest上使用.abort()

var req = $.get('ajax/test.html', function(data) {
            $('.result').html(data);
            alert('Load was performed.');
          });

//Abort request
req.abort()

From jQuery.ajax() documentation: 来自jQuery.ajax()文档:

The $.ajax() function returns the XMLHttpRequest object that it creates. $ .ajax()函数返回它创建的XMLHttpRequest对象。 Normally jQuery handles the creation of this object internally, but a custom function for manufacturing one can be specified using the xhr option. 通常,jQuery在内部处理此对象的创建,但可以使用xhr选项指定用于制造该对象的自定义函数。 The returned object can generally be discarded, but does provide a lower-level interface for observing and manipulating the request. 返回的对象通常可以被丢弃,但确实提供了一个用于观察和操作请求的低级接口。 In particular, calling .abort() on the object will halt the request before it completes. 特别是,在对象上调用.abort()将在请求完成之前暂停该请求。

More on .abort() : XMLHttpRequest.abort() 有关.abort()更多信息: XMLHttpRequest.abort()

There is also jQuery plugin AjaxQueue for handling multiple Ajax request made in parallel. 还有jQuery插件AjaxQueue用于处理并行发出的多个Ajax请求。

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

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