[英]JQuery Autocomplete caching with ajax not populating
我試圖通過ajax調用添加緩存到下面的代碼。 在我第一次自動完成工作時添加緩存后,第二次顯示空白時,代碼可以正常緩存。 我在這做錯了什么?
我的代碼
$(document).ready(function () {
$("#MainContent_txtSurname").autocomplete({
source: function (request, response) {
var term = request.term;
if (term in cache) {
response(cache[term]);
return;
}
$.ajax({
crossDomain: true,
type: 'POST',
url: "http://localhost:1448/GetSurnames",
dataType: 'json',
data: { "Name": request.term, "CID": CID },
processdata: true,
success: function (result) {
var Surnames = JSON.parse(result.data);
cache[term] = Surnames;
response($.map(Surnames, function (item) {
return {
label: item.homename,
value: item.homename
}
}));
},
error: function (a, b, c) {
debugger;
}
});
},
minLength: 2
});
});
返回的數據是:
{"data":"[{\"id\":3,\"homename\":\"D\\u0027Costa\"}]"}
嘗試緩存自動完成插件的正確格式數據。 關於你的Ajax成功:
success: function (result) {
var Surnames = JSON.parse(result.data);
cache[term] = $.map(Surnames, function (item) {
return {
label: item.homename,
value: item.homename
}
});
response(cache[term]);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.