【问题标题】:jquery ui autocomplete needs additional functionjquery ui自动完成需要额外的功能
【发布时间】:2011-05-28 04:46:01
【问题描述】:

我有这样的自动完成代码:

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

     });
  }

});

当我尝试获取数据时,Firebug 显示错误:“updateSpotList 未定义”。 我需要创建一个名为 updateSpotList 的函数来从服务器获取响应。并且永远不会调用成功警报。

为什么我需要这个功能?也许在aspx中定义了一些东西?它是:

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 = DataBridge.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);
            }
        }
    }

【问题讨论】:

    标签: asp.net jquery user-interface autocomplete jquery-autocomplete


    【解决方案1】:

    您可以指定 URL 作为源参数,而不是数据参数。

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

    插件将向您的服务器发出请求,您应该返回一个如下所示的 JSON 数组:

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

    将您的 JavaScript 代码更改为:

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

    自动完成插件会在源 URL 中附加一个名为 term 的参数,其中包含输入元素的当前值,因此如果您写了,则会向“AjaxSearch.aspx?a=getspots&c=updateSpotList&term=test”发出请求在输入元素中测试。

    在服务器上,您想将 q 更改为 term。

    【讨论】:

    • 如果您的意思是将 url 更改为 AjaxSearch.aspx?a=getspots&c=updateSpotList&q=request.term 并删除数据,那么问题仍然存在。
    • 我没有。不要使用数据:使用来源:AjaxSearch.aspx?a=getspots&c=updateSpotList 然后它会自动将术语添加到 URL 中。
    • 谢谢,但这并没有解决我的问题。如果我使用“源”对服务器的请求根本没有创建,并且如果“url”那么 updateSpotList 需要像以前一样。
    • 试试我刚刚更新的代码。然后您可能需要将响应从 JSONP 更改为常规 JSON 数组。
    • 这是漏洞问题 - 我无法在服务器上更改文件。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多