Apologies for posting the ten billionth jQuery autocomplete question...
I'm having trouble with a jQuery UI autocomplete textbox. I'm unsure if I'm doing the right thing client-side to re-populate the autocomplete data source following keystrokes.
The javascript in my aspx page is as follows:
$(function() {
$("#<%=txtAuthorityName.ClientID%>").autocomplete({
minLength: 2,
delay: 0,
dataType: "json",
search: function(data) {
$.getJSON("AuthoritySearchHandler.ashx?SearchTerms=" + $("#<%=txtAuthorityName.ClientID%>").val() + "&AuthorityType=" + $("#<%=ddlSector.ClientID%>").val(), function(data) {
$("#<%=txtAuthorityName.ClientID%>").autocomplete("option", "source", data);
})
}
});
});
and the code in my ashx handler is as follows:
public void ProcessRequest(HttpContext context)
{
string searchTerms = context.Request["SearchTerms"] ?? string.Empty;
string authorityType = context.Request["AuthorityType"];
int authorityTypeId = 0;
string json = "";
if (int.TryParse(authorityType, out authorityTypeId) && authorityTypeId > 0 && searchTerms.Length > 0)
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
var authorities = from a in BusinessLayer.SearchAuthorities(authorityTypeId, searchTerms)
select a.Name;
json = serializer.Serialize(authorities);
}
context.Response.ContentType = "application/json";
context.Response.Write(json);
context.Response.End();
}
I'm fairly sure the ashx handler is doing what it should (I've inspected the HTTP responses using fiddler to be sure). I'm getting the error "Microsoft JScript runtime error: Object expected"?