Jason
Jason

Reputation: 11615

JQuery UI Autocomplete and Generic Handler (ashx) - C# ASP.NET

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?

Upvotes: 1

Views: 5429

Answers (2)

BrokenGlass
BrokenGlass

Reputation: 160862

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();
    }

Upvotes: 6

Andrew
Andrew

Reputation: 14447

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.

Upvotes: 1

Related Questions