EvanGWatkins
EvanGWatkins

Reputation: 1457

jQuery UI autocomplete is not displaying results fetched via AJAX

I am trying to use the jQuery UI autocomplete feature in my web application. What I have set up is a page called SearchPreload.aspx. This page checks for a value (term) to come in along with another parameter. The page validates the values that are incoming, and then it pulls some data from the database and prints out a javascript array (ex: ["item1","item2"]) on the page. Code:

protected void Page_Load(object sender, EventArgs e)
{
    string curVal;
    string type ="";
    if (Request.QueryString["term"] != null)
    {
        curVal = Request.QueryString["term"].ToString();
        curVal = curVal.ToLower();
        if (Request.QueryString["Type"] != null)
            type = Request.QueryString["Type"].ToString();
        SwitchType(type,curVal);
    }
}
public string PreLoadStrings(List<string> PreLoadValues, string curVal)
{
    StringBuilder sb = new StringBuilder();
    if (PreLoadValues.Any())
    {
        sb.Append("[\"");
        foreach (string str in PreLoadValues)
        {
            if (!string.IsNullOrEmpty(str))
            {
                if (str.ToLower().Contains(curVal))
                    sb.Append(str).Append("\",\"");
            }
        }
        sb.Append("\"];");
        Response.Write(sb.ToString());
        return sb.ToString();
    }
}

The db part is working fine and printing out the correct data on the screen of the page if I navigate to it via browser.

The jQuery ui autocomplete is written as follows:

$(".searchBox").autocomplete({
    source: "SearchPreload.aspx?Type=rbChoice",
    minLength: 1
});

Now if my understanding is correct, every time I type in the search box, it should act as a keypress and fire my source to limit the data correct? When I through a debug statement in SearchPreload.aspx code behind, it appears that the page is not being hit at all.

If I wrap the autocomplete function in a .keypress function, then I get into the search preload page but still I do not get any results. I just want to show the results under the search box just like the default functionality example on the jQuery website. What am I doing wrong?

Upvotes: 0

Views: 1101

Answers (2)

Salman Arshad
Salman Arshad

Reputation: 272086

autocomplete will NOT display suggestions if the JSON returned by the server is invalid. So copy the following URL (or the returned JSON data) and paste it on JSONLint. See if your JSON is valid.

http://yourwebsite.com/path/to/Searchpreload.aspx?Type=rbChoice&term=Something

PS: I do not see that you're calling the PreLoadStrings function. I hope this is normal.

Upvotes: 3

szeliga
szeliga

Reputation: 621

A couple of things to check.

HTH.

Upvotes: 0

Related Questions