我试图通过以下代码的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]);
}