开发者

jquery ui autocomplete needs additional function

开发者 https://www.devze.com 2023-01-31 22:49 出处:网络
I have such autocomplete code: $(\"input#PickupSpot\").autocomplete({ source: function(request, response){

I have such autocomplete code:

$("input#PickupSpot").autocomplete({
  source: function(request, response){
     $.ajax({
        url: "AjaxSearch.aspx",
        dataType: "jsonp",
        data: {
           a: "getspots",
           c: "updateSpotList",
           q: request.term
        },
        success: function(){
           alert("Success");
        },
        error: function (xhr, ajaxOptions, thrownError){
                alert(xhr.status);
                alert(thrownError);
        }

     });
  }

});

When i try to get data Firebug shows an error: "updateSpotList is not defined". I need to creat a function called updateSpotList to get response from the server. And the success alert is never invoked.

Why do i need this function? Maybe there are something defined in aspx? It is:

string response = "";

 string callback = Request.QueryString["c"];
 string action = Request.QueryString["a"]; 
 string query = Request.QueryString["q"];

  //Ensure action parameter is set
  if(action != null && action.Equals("getspots")){
        //Ensure query parameter is set
        if (query != null && query.Length > 0){
            SpotListRequest request = new SpotListRequest
            {
                FreeText = query,
                Language = "sv-SE"
            };
            IEnumerable<Spot> spots = DataBrid开发者_StackOverflow社区ge.GetSpotList(null, null, query).OrderBy(i => i.FullName);

            JavaScriptSerializer js = new JavaScriptSerializer();

            string json = js.Serialize(spots.ToArray());

            //Ensure callback parameter is set
            if (callback != null && callback.Length > 0)
            {
                response = String.Format("{0}('{1}')", callback, json);
            }
        }
    }


You can specify a URL as the source parameter, instead of the data parameter.

http://jqueryui.com/demos/autocomplete/#option-source

The plugin will make a request to your server, and you should return a JSON array looking like this:

[{label: "Name"}, {label: "Name 2"}]

Change your JavaScript code to this:

$("input#PickupSpot").autocomplete({
  source: "AjaxSearch.aspx?a=getspots&c=updateSpotList"
});

The autocomplete plugin will append a parameter named term to the source URL, with the current value of the input element, so a request would be made to: "AjaxSearch.aspx?a=getspots&c=updateSpotList&term=test" if you wrote test in the input element.

On the server, you want to change q to term.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号