1

I'm trying to use the JQuery Autocomplete, but I guess I am having trouble getting the format it expects from my handler.

Here is what the handler does. This was in another SO question....

 context.Response.ContentType = "text/plain";
 var companies = GetCompanies(); //This returns a list of companies (List<string>)

 foreach (var comp in companies)
 {
     context.Response.Write(comp + Environment.NewLine);
 }

This doesn't work. It is definately getting called and it is returning what I would expect this code to return. Any ideas?

Jason
  • 11,435
  • 24
  • 77
  • 131

2 Answers2

6

It needs to be in JSON format indeed, here a sample of the general outline I used before:

    class AutoCompleteEntry
    {
        public int id { get; set; }
        public string label { get; set; }
        public string value { get; set; }
    }

    private void GetAutoCompleteTerms()
    {
        Response.Clear();
        Response.ContentType = "application/json";

        //evaluate input parameters of jquery request here

         List<AutoCompleteEntry> autoCompleteList= new List<AutoCompleteEntry>();
        //populate List of AutocompleteEntry here accordingly

        JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
        string json = jsSerializer.Serialize(autoCompleteList);
        Response.Write(json);
        Response.End();
    }
BrokenGlass
  • 158,293
  • 28
  • 286
  • 335
1

The response needs to be in JSON format. See http://docs.jquery.com/UI/Autocomplete where it discusses using a String that specifies a URL.

Andrew
  • 14,325
  • 4
  • 43
  • 64
  • So then I am assuming http://stackoverflow.com/questions/305994/jquery-autocomplete-and-asp-net is using a plugin instead of the JQuery UI... OOps – Jason Nov 05 '10 at 16:53