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

相關問題