0

我有这样的自动完成代码:jQuery用户界面自动完成需要额外的功能

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

回答

1

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

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

该插件会主动要求到您的服务器,你应该返回一个JSON数组看起来像这样:

[{标签: “姓名”},{标签: “名称2” }]

JavaScript代码改成这样:

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

自动完成插件将一个参数命名项追加到源URL,与CUR如果您在输入元素中写入测试,则会请求:“AjaxSearch.aspx?a = getspots & c = updateSpotList & term = test”。

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

+0

如果您的意思是将url更改为AjaxSearch.aspx?a = getspots&c = updateSpotList&q = request.term并删除数据,则问题仍然存在。 – UngaBunga 2010-12-20 12:09:47

+0

我不知道。不要使用数据: 使用来源:AjaxSearch.aspx?a = getspots&c = updateSpotList 然后它会自动添加到URL的术语。 – MartinHN 2010-12-20 15:11:36

+0

我已经更新了我的答案,以便更详细。 – MartinHN 2010-12-21 07:21:44

相关问题