I implemented an ajax autocomplete feature in ASP.NET where a method from a web service is called to fetch postal codes.
public string[] GetNames(string prefixText, int count, String contextKey)
{
prefixText = prefixText.Trim();
XmlNodeList list;
List<string> names = new List<string>();
if ((prefixText[0] >= '0') && (prefixText[0] <= '9'))
{
if ((contextKey == null) || (contextKey.Equals("")))
list = cpsForAgences["groupe"];
else
list = cpsForAgences[contextKey];
int i=0;
foreach (System.Xml.XmlNode node in list)
{
if (node.InnerText.ToLower().StartsWith(prefixText))
{
names.Add(node.InnerText);
if (++i >= count)
break;
}
}
names.Sort();
return names.ToArray();
}
}
When the client attempts to publish the responses, Sys.Serialization.JavaScriptSerializer.deserialize() is called:
try {
var pair = Sys.Serialization.JavaScriptSerializer.deserialize('(' + completionItems[i] + ')');if (pair && pair.First) {
text = pair.First;value = pair.Second;} else {
text = pair;value = pair;}
}
However, there seems to be a discrepancy for postal codes that start with '0' between the result returned by Sys.Serialization.JavaScriptSerializer.deserialize and the value of completionItems[i]. How can this behavior be rectified? Thank you!