views:

526

answers:

3

I am implementing a jquery autocomplete on a search form and am getting the suggestions from the Google Search Appliance Autocomple suggestions service which returns a result set in json.

What I am trying to do is go off to the GSA to get suggestions when the user types something in the search box.

The url to get the json suggestions is as follows:

http://gsaurl/suggest?q=<query>&max=10&site=default_site&client=default_frontend&access=p&format=rich

The json which is returned is as follows:

{ "query":"re", "results": [ {"name":"red", "type":"suggest"}, {"name":"read", "type":"suggest"}] }

The jQuery autocomplete code is as follows:

$(#q).autocomplete(searchUrl, {
width: 320,
dataType: 'json',
highlight: false,
scroll: true,
scrollHeight: 300,
parse: function(data) {
 var array = new Array();

 for(var i=0;i<data.results.length;i++)
 {
  array[i] = { data: data.results[i], value: data.results[i].name, result: data.results[i].name };
 }

 return array;
},
formatItem: function(row) { 
 return row.name;
}

});

This works in IE but fails in firefox as the data returned in the parse function is null. Any ideas why this would be the case?

Workaround

I created an aspx page to call the GSA suggest service and to return the json from the suggest service. Using this page as a proxy and setting it as the url in the jQuery autocomplete worked in both IE and FireFox.

+1  A: 

Workaround

I created an aspx page to call the GSA suggest service and to return the json from the suggest service. Using this page as a proxy and setting it as the url in the jQuery autocomplete worked in both IE and FireFox.

Proxy code

string responseText;

            try
            {
                Uri gsaUrl = new Uri(GetSuggestUrl());

                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(gsaUrl);
                request.ContentType = "application/x-www-form-urlencoded";
                request.Method = WebRequestMethods.Http.Get;                

                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Stream responseStream = response.GetResponseStream();
                StreamReader streamReader = new StreamReader(responseStream);

                responseText = streamReader.ReadToEnd();

            }
            catch(Exception e)
            {
                throw new Exception(e.Message, e.InnerException);
            }           

            string json = responseText;
            Response.Clear();
            //Response.ContentType = "application/json; charset=utf-8";
            Response.Write(json);

        }

        private string GetSuggestUrl()
        {
            string url = "http://&lt;GSA&gt;/suggest";
            string query = HttpContext.Current.Request.QueryString["q"];
            int max = 10;
            string site = "site";
            string client = "client";
            string access = "p";
            string format = "rich";

            return string.Format("{0}?q={1}&max={2}&site={3}&client={4}&access={5}&format={6}", url, query, max, site, client, access, format); 
        }
skyfoot
A: 

Would you be willing to post your code for the .net proxy?

DaMorgue
I have added the proxy code to my answer
skyfoot
thanks! It would be great if you could get to the data using jsonp. Maybe the GSA will support this in the near future.
DaMorgue
A: 

Is there any options Google search appliance to return JSONP format?