避免以前的ajax请求在jquery中再次调用相同的ajax请求 [英] Avoid previous ajax request on calling the same ajax request again in jquery
本文介绍了避免以前的ajax请求在jquery中再次调用相同的ajax请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在jquery中再次调用相同的ajax请求时避免以前的ajax请求
I want to avoid previous ajax request on calling the same ajax request again in jquery
<input type="text" id="username" onkeyup = "userName(this)" >
function userName(e) {
$.ajax({
type:"POST",
url:BasePath + "/users/userName",
data:"name=" + $.trim($(e).val()),
success:function (resp) {
if (resp == 1) {
$('#div.error_message').html('Username already taken...').show();
} else {
$('#div.error_message').hide();
}
}
});
}
推荐答案
你可以使用.abort( )
You can use .abort()
<input type="text" id="username" onkeyup = "userName(this)" >
<script>var xhr=null;
function userName(e) {
if(xhr)xhr.abort();//first time it will not be aborted
xhr=$.post(BasePath + "/users/userName",{"name": $.trim($(e).val())},function (resp) {
if (resp == 1) {
$('#div.error_message').html('Username already taken...').show();
} else {
$('#div.error_message').hide();
}
});
}</script>
使用$ .post代替$ .ajax,因为它是简化的$ .ajax的缩写形式
Use $.post in place of $.ajax as it is simplified and short form of $.ajax
这篇关于避免以前的ajax请求在jquery中再次调用相同的ajax请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文