gpt4 book ai didi

ajax - Mvc 自动完成 ajax

转载 作者:行者123 更新时间:2023-12-04 03:19:29 24 4
gpt4 key购买 nike

尝试:

$(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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com