尝试:
$(document).ready(function () {
$('#cityName').autocomplete({
source: function(request,response) {
$.ajax({
type: 'POST',
url: '@Url.Action("Search", "City")',
dataType: 'json',
data: { name: request.term } ,
success: function (data) {
response($.map(data, function (item) {
alert(JSON.stringify(data));
alert(JSON.stringify(item.name));
return {
name: item.name,
label: item.name
}
}));
}
})
},
messages: {
noResults: "", results: ""
}
})
})
在 alert(JSON.stringify(data))
中得到了这个:{"items":["Boston","Berlin"]}。
在 alert(JSON.stringify(item.name))
中得到了这个:undefined。
问题:它 (item.name) 是如何工作的?
最佳答案
你必须只返回字符串数组:
$(document).ready(function () {
$('#cityName').autocomplete({
source: function(request,response) {
$.ajax({
type: 'POST',
url: '@Url.Action("Search", "City")',
dataType: 'json',
data: { name: request.term } ,
success: function (data) {
response(data.items);
}
})
},
messages: {
noResults: "", results: ""
}
})
})
关于ajax - Mvc 自动完成 ajax,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39270803/