自动完成ajax请求只有当项目从以前的ajax请求数据不匹配
Jquery Autocomplete ajax request only if item from previous ajax request data is not matching
我使用jQuery自动完成。每次按键都会发出AJAX请求,这不是我想要的。如果以前的AJAX请求中的数据与搜索匹配,则不应再发出任何AJAX请求。
<script>
$('#tags').autocomplete({
source: function (request, response) {
$.ajax({
url: '/TestDDl/Index',
// data: "{ 'prefix': '" + request.term + "'}",
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
success: function (data) {
response($.map(data.user, function (item) {
return {
label: data.name,
val: data.val
}
}))
},
error: function (response) {
alert(response.responseText);
},
failure: function (response) {
alert(response.responseText);
}
});
},
autoFocus: true,
keypress: function (event, ui) {
alert('Not Selected');
if (!ui.item) {
alert('Not Selected');
}
}
});
});
在这里,如果我输入的用户名已经在以前的AJAX请求数据中,它不应该在接下来的按键上发出AJAX请求。
您可以声明一个变量,将用户输入传递给它,然后在成功函数中更新它。在进行下一次调用之前,检查变量是否与下一个数据匹配。
像这样:
<script>
var recent = '';
$('#tags').autocomplete({
source: function (request, response) {
if (recent == request.term) {
return;
}
recent = request.term;
$.ajax({
url: '/TestDDl/Index',
data: "{ 'prefix': '" + request.term + "'}",
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
success: function (data) {
response($.map(data.user, function (item) {
return {
label: data.name,
val: data.val
}
}))
},
error: function (response) {
alert(response.responseText);
},
failure: function (response) {
alert(response.responseText);
}
});
},
autoFocus: true,
keypress: function (event, ui) {
alert('Not Selected');
if (!ui.item) {
alert('Not Selected');
}
}
});
</script>
<script>
window.xyz = []; //For saving history
$('#tags').autocomplete({
source: function (request, response) {
if(xyz[request]){
response(xyz[request]); // Return previously saved data
}else(
$.ajax({
url: '/TestDDl/Index',
// data: "{ 'prefix': '" + request.term + "'}",
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
success: function (data) {
var res = $.map(data.user, function (item) {
return {
label: data.name,
val: data.val
}
});
xyz[request]=res; //set data to reuse later
response(res)
},
error: function (response) {
alert(response.responseText);
},
failure: function (response) {
alert(response.responseText);
}
});
}
},
autoFocus: true,
keypress: function (event, ui) {
alert('Not Selected');
if (!ui.item) {
alert('Not Selected');
}
}
});
</script>
下一个ajax请求应该在第一个ajax请求的Success函数中。在那里建立逻辑。如果你找到匹配,不要调用第二个ajax请求,否则调用第二个ajax请求,所有这些都应该在第一个ajax请求的成功函数
相关文章:
- ajax请求的顺序总是不同的
- 从ajax请求中获取javascript对象
- Ajax请求文档就绪会导致jquery加载缓慢
- MockJax没有在JavaScript应用程序中发送对我AJAX请求的响应
- 正在传递JSONP标头's数据参数到另一个文件中的AJAX请求
- JavaScript代码未正确检查ajax请求
- node.js请求数据事件未在CORS ajax调用中触发
- jQuery Ajax GET请求工作不正常
- "日期“;AJAX请求返回的类型值未定义
- Django - 响应请求 AJAX
- 使用 jQuery 的 CORS 请求 - $.ajax()
- 如果无线电值为 ==1,则请求 ajax
- 如何处理对循环中发出的多个异步请求(AJAX 调用)的响应
- POST请求ajax jquery错误
- CasperJS don'我没有请求AJAX
- 使用这种Facebook风格的Lightbox-请求AJAX-在一个页面上覆盖多个按钮
- 400错误请求ajax post请求
- 如何在开始发送请求ajax之前延迟3秒
- 与jQuery同时请求AJAX
- 使用POST请求AJAX发送json对象