我不確定爲什麼會發生這種情況,但是當我的頁面加載時,會立即爲搜索結果發出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