【发布时间】:2016-09-01 11:50:34
【问题描述】:
尝试:
$(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)) 得到这个:未定义。
问题:它 (item.name) 是如何工作的?
【问题讨论】:
-
试试
items[0]- 数据是对象 -
它有效,但仅适用于名字。
-
你必须使用每个循环,遍历所有元素
-
是的,但是你的ajax调用需要什么样的输出,如果它可以是一个字符串数组,你可以直接返回
item -
@Sousuke 这是一个字符串列表,但如果我尝试返回
item,我会在同一行得到“波士顿,柏林”。
标签: ajax asp.net-mvc jquery-ui-autocomplete