我不确定为什么会发生这种情况,但是当我的页面加载时,会立即为搜索结果发出XHR请求。它对用户是不可见的,但它加载了相当大的json数据块。jQuery AJAX在页面加载时加载json内容,宁愿在搜索框中输入时加载json内容
这里是我的代码:
$.ajax({
type: "POST",
url: "http://localhost:8888/index.php/ajax/get_client",
dataType: "json", data: "{}",
success: function(data) {
$('#search').autocomplete({
source:data,
minLength:2,
delay:0,
appendTo:'header',
selectFirst:true,
select:function(event, ui) {
$("input#search").val(ui.item.value);
$("#search").closest('form').submit();
}
});
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert(errorThrown);
}
});
我怎样才能使它所以JSON数据仅要求当用户在输入#搜索框?
太棒了,谢谢。说得通。 – dallen 2012-02-02 23:42:32