jQuery AutoComplete Plugin not working for JSON Response (sValue.substring is not a function)
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!
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 + ''
精彩评论