【发布时间】:2013-06-03 11:41:05
【问题描述】:
我正在尝试使用 jQuery AutoComplete,但我得到的只是一个包含所有结果的单个项目。
我有这个 ASP.Net WebMethod:
[WebMethod]
public static string FetchCompletionList(string term)
{
var json = JsonConvert.SerializeObject(CustomerProvider.FetchKeys(term, 8));
return json;
}
被此脚本调用:
$("[id$='txtLKey']").autocomplete({
minlength: 2,
source: function(request, response) {
$.ajax({
type: "POST",
url: "/Views/Crm/Json/Json.aspx/FetchCompletionList",
data: '{term: "' + $("[id$='txtLKey']") .val() + '", count: "8"}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
response(data);
},
error: function (jqXHR, textStatus, errorThrown) {
alert(jqXHR.responseText);
alert(textStatus);
alert(errorThrown.toString());
}
});
}
});
结果是这样的:
当我真正想要的是用户可以选择的选项列表时,即每个 NZ 应该是列表中的一个项目。
【问题讨论】:
-
可以分享一下服务器返回的值吗?在浏览器中使用 firebug/developer 工具查看响应
-
看那是我不明白的。没有标签/值,它只是一个字符串数组而不是字典。文档jqueryui.com/autocomplete/#default 将字符串数组作为源输入到小部件中。为什么远程返回的数据需要有字典形式的额外“无意义”绒毛?据我所见,文档没有发现任何需要为远程返回的数据使用不同的格式。谢谢
-
对不起,我的陈述是错误的,自动完成在内部标准化了传递给
response的输入。那么原因可能是data可能不是一个数组,它可能是一个字符串。你能用console.log(data)看看打印的是什么 -
{"d":"[\"NZ0008\",\"NZ0015\",\"NZ0017\",\"NZ0018\",\"NZ0026\",\"NZ0027\" ,\"NZ0031\",\"NZ0035\"]"} 来自萤火虫
-
这里有两个问题,1.你需要通过
data.d响应response(data.d);你的值字符串是无效的应该是{"d":["NZ0008","NZ0015","NZ0017","NZ0018","NZ0026","NZ0027","NZ003??1","NZ0035"]}