jQuery AutoComplete 插件不适用于 JSON 响应(sValue.substring 不是函数)
我正在尝试使用 jQuery 的自动完成插件(这个 http://docs.jquery.com/插件/自动完成)。我的服务器正在返回 JSON 字符串,我尝试通过 AutoComplete 插件的“parse
”和“formatItem
”参数在客户端上处理该字符串,如下所示:
$(document).ready(function()
{
$('.searchBox input.textbox').autocomplete('/DoSearch.aspx',
{
mustMatch: false,
autoFill: true,
minChars: 1,
dataType: 'json',
parse: function(data)
{
var array = new Array();
for (var i = 0; i < data.length; i++)
{
array[array.length] = { data: data[i], value: data[i].ID, result: data[i].ID };
}
return array;
},
formatItem: function(row, i, n)
{
return row.ID + ': ' + row.Title;
}
});
});
当我运行此命令时我在 Firebug 中收到“sValue.substring is not a function”错误。但是,如果我在 formatItem 和解析函数上粘贴断点,它们会按预期命中并包含有效数据。
这是从服务器返回的 JSON 文本的精确复制“n”粘贴:
[{"ID":140177,"Title":"Food Handling","Code":"J01.576.423.200"},{"ID":140178,"Title":"Food Handling","Code":"J01.576.423.200"},{"ID":140179,"Title":"Brain Infarction","Code":"C10.228.140.300.301.200"},{"ID":140180,"Title":"Cerebral Hemorrhage","Code":"C10.228.140.300.535.200"},{"ID":140182,"Title":"Insulin","Code":"D06.472.610.575"},{"ID":140183,"Title":"Insulin","Code":"D06.472.610.575"},{"ID":140184,"Title":"Insulin","Code":"D06.472.610.575"},{"ID":140186,"Title":"Insulin","Code":"D06.472.610.575"},{"ID":140188,"Title":"Insulin","Code":"D06.472.610.575"},{"ID":140189,"Title":"Sulfonylurea Compounds","Code":"D02.886.590.795"}]
请帮忙,我已经在 Google 和 StackOverflow 中搜索了帮助,但找不到其他人遇到此错误,干杯!
I'm trying to use the autocomplete plugin for jQuery (this one http://docs.jquery.com/Plugins/Autocomplete). My server is returning JSON string, which I'm trying to process on the client via AutoComplete plugin's 'parse
' and 'formatItem
' parameters, like so:
$(document).ready(function()
{
$('.searchBox input.textbox').autocomplete('/DoSearch.aspx',
{
mustMatch: false,
autoFill: true,
minChars: 1,
dataType: 'json',
parse: function(data)
{
var array = new Array();
for (var i = 0; i < data.length; i++)
{
array[array.length] = { data: data[i], value: data[i].ID, result: data[i].ID };
}
return array;
},
formatItem: function(row, i, n)
{
return row.ID + ': ' + row.Title;
}
});
});
When I run this I get a 'sValue.substring is not a function' error thrown in Firebug. However, if I stick breakpoints on formatItem and parse function, they are hit as expected and contain valid data it seems.
Here is an exact copy 'n' paste of the JSON text that gets returned from the server:
[{"ID":140177,"Title":"Food Handling","Code":"J01.576.423.200"},{"ID":140178,"Title":"Food Handling","Code":"J01.576.423.200"},{"ID":140179,"Title":"Brain Infarction","Code":"C10.228.140.300.301.200"},{"ID":140180,"Title":"Cerebral Hemorrhage","Code":"C10.228.140.300.535.200"},{"ID":140182,"Title":"Insulin","Code":"D06.472.610.575"},{"ID":140183,"Title":"Insulin","Code":"D06.472.610.575"},{"ID":140184,"Title":"Insulin","Code":"D06.472.610.575"},{"ID":140186,"Title":"Insulin","Code":"D06.472.610.575"},{"ID":140188,"Title":"Insulin","Code":"D06.472.610.575"},{"ID":140189,"Title":"Sulfonylurea Compounds","Code":"D02.886.590.795"}]
Please help, I've already searched Google and StackOverflow for help, but can't find anyone having else this error, cheers!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该死!
我找到了问题的原因。这是因为我的 JSON 字符串中的 ID 值输入为整数而不是字符串,例如:
{"ID":140177,
而不是{"ID":"140177", 和 AutoComplete 假设所有内容都是字符串。
我通过在服务器上的 ID 值周围加上引号解决了这个问题,或者只是在“解析”函数中将其转换为客户端的字符串,如下所示:
value: data[i].ID + '', result: data [i].ID + ''
Dammit!!!
I found the cause of the problem. It's because my ID value in my JSON string was typed as a Integer rather than a String eg:
{"ID":140177,
instead of{"ID":"140177",
and AutoComplete assumes everything is going to be a string.I fixed the problem by wrapping quotes around the ID value on the server, or just convert it into a string clientside in the 'parse' function like so:
value: data[i].ID + '', result: data[i].ID + ''