Result not showing

Alec's Avatar

Alec

14 Oct, 2012 11:43 PM

I am trying to use this AutoSuggest plugin from JQuery, together with a HttpHandler as follow. It seems to be working except that it's always saying no result. I used Firebug to check the data returned from the handler and it did return the data, but somehow there's a Javascript error as follow. Does anyone know why?

public void ProcessRequest(HttpContext context)

    { 
        if (!string.IsNullOrEmpty(context.Request["q"])) 
        { 
            context.Response.ContentType = "application/json"; 
            var obj = new List<string>(); 
            List<Suburb> result = getSuburbs(context.Request["q"], 10); 

            string[] suburbs = new string[result.Count]; 
            int c = 0; 
            foreach (Suburb suburb in result) 
            { 
                suburbs[c] = suburb.Title; 
                c++; 
            } 

            System.Web.Script.Serialization.JavaScriptSerializer jss = new System.Web.Script.Serialization.JavaScriptSerializer(); 
            string data = jss.Serialize(suburbs); 
            context.Response.Write(data); 

        } 
    }

Firebug response for a "gl" input is:
["GLENELG","GLEN OSMOND","GLYNDE"] but the plugin shows as no result returned with a Js error in firebug:

TypeError: control is null if (typeof(control.value) == "string") {

  1. 1 Posted by Jon on 15 Oct, 2012 06:29 AM

    Jon's Avatar

    As you can see here, your handler must return something like:

    var data = {items: [
        {value: "1", name: "GLENELG"},
        {value: "2", name: "GLEN OSMOND"},
        {value: "3", name: "GLYNDE"},
    ]};
    
  2. 2 Posted by Alec Tang on 15 Oct, 2012 06:37 AM

    Alec Tang's Avatar

    Erm... How can I wrap up the code to return in such format? Thanks a lot...

    Sent from my iPhone

  3. 3 Posted by Jon on 15 Oct, 2012 07:06 AM

    Jon's Avatar

    You have to change your foreach loop. I dont know ASP but i suppose you can do something like this (in a string variable):

    string_to_return = "[";
    foreach (Suburb suburb in result) { 
        if (c > 1){
           string_to_return = string_to_return & ",";
        }
        string_to_return  = string_to_return & '{"value":"' & c & '","name":"' & suburb.Title & '"}'; 
        c++; 
    }
    string_to_return = string_to_return & "]";
    context.Response.Write(string_to_return);
    
  4. 4 Posted by Alec on 15 Oct, 2012 12:41 PM

    Alec's Avatar

    Thanks Jon, I modified the code to output this response:

    items: [{value: "43894", name:"GLENELG - 5045 SA"},{value: "43946", name:"GLEN OSMOND - 5064 SA"},]

    But now, it just do nth... there's no selection appearing below the textbox...

    I noticed there's a js error with the response data:

    TypeError: control is null
    [Break On This Error]

    if (typeof(control.value) == "string") {

  5. 5 Posted by Jon on 15 Oct, 2012 01:39 PM

    Jon's Avatar

    Try to output a response like this:

    [{"value": "43894", "name": "GLENELG - 5045 SA"},
     {"value": "43946", "name":"GLEN OSMOND - 5064 SA"}]
    

    with the words name and value between quotation marks, and without the last comma before the last bracket.

    What are the values for selectedItemProp, selectedValuesProp and searchObjProps parameters?

    Don't know where that error come from, It may be related with the way you initialize the autosuggest plugin. I can't help you on the ASP part.

  6. 6 Posted by Alec on 15 Oct, 2012 01:58 PM

    Alec's Avatar

    Thanks a lot for the reply...

    Erm... I am already returning this:

    {items: [ {value: "43894", name:"GLENELG - 5045 SA"}, {value: "43946", name:"GLEN OSMOND - 5064 SA"}, {value: "43985", name:"GLYNDE - 5070 SA"} ]}

    Do i need to wrap it in {items: } ?

    My initialisation is:

    $("#<%=txbSuburb.ClientID %>").autoSuggest("<%=SiteUrl %>/GetSuburbs.ashx", { minChars: 2, matchCase: false });

    And I left the js file untouched, so it is:

    selectedItemProp: "value",
    selectedValuesProp: "value",
    searchObjProps: "value",

  7. 7 Posted by Alec on 16 Oct, 2012 12:40 AM

    Alec's Avatar

    I noticed that as soon as I use a url to retrieve data, it stopped working... even the data is the same...

  8. 8 Posted by Alec on 16 Oct, 2012 12:52 AM

    Alec's Avatar

    This is what I get... data returned, but no auto suggest

  9. 9 Posted by Jon on 16 Oct, 2012 10:27 AM

    Jon's Avatar

    I can't help you with the example you posted, I don't know ASP.

    This is the data I'm using (without {items} and with value and name between quotation marks):

    [{"value":"52","name":"Uriarte Bercedo , Jon"},{"value":"todos","name":"Todos"}]
    

    Firebug doesn't recognize it as JSON.

  10. 10 Posted by Alec on 16 Oct, 2012 01:51 PM

    Alec's Avatar

    Hi Jon,

    I've finally got it to work!!!! I need to put double quotes around value and name labels as your last example...

    Thanks a lot!!!

  11. 11 Posted by Jon on 16 Oct, 2012 02:56 PM

    Jon's Avatar

    Jejje,

    glad to have been of help!

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:

»

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac