2009-10-26 60 views
1

我正在使用JQuery自動完成插件,並將字符串數組傳遞給自動完成(請參閱下面的代碼)。我打電話來獲取我的數據(GetCustomerNames)的方法只是返回一個字符串數組,而且工作正常。我需要找到一些方法將參數傳遞給GetCustomerNames方法,以便可以過濾返回的內容。有人能幫忙嗎?AutoComplete JQuery插件和ASP.Net C#

這裏是Default.aspx頁面的標記代碼:

<head runat="server"> 
<title></title> 
<script type="text/javascript" src="js/jquery-1.3.2.js" ></script> 
<script type="text/javascript" src="js/jquery.autocomplete.js" ></script> 
<script type="text/javascript"> 
    //Working, but uses results output to an aspx page using StringBuilder, trying 
    //to find a way to get the data with json 
    //$(document).ready(function() { 
     // $("#example").autocomplete('AutoCompleteData.aspx'); 

    //}); 
    $(document).ready(function() { 
     $("#example").keyup(function() { 
      $.ajax({ 
       type: "POST", 
       url: "Default.aspx/GetCustomerNames", 
       data: "{}", 
       contentType: "application/json; charset=utf-8", 
       dataType: "json", 
       success: function(msg) {  
        $("#example").autocomplete(msg.d); 
       }, 
       error: function(msg) { 
        alert("error"); 
       } 
      });  
     }); 
    });  
</script> 


客戶名稱:

這裏是在Default.aspx.cs代碼執行GetCustomerNames方法的頁面後面的代碼:

[WebMethod] 
public static string[] GetCustomerNames() 
{ 
    string[] data = new string[] {"Andrew", "Ramona", "Russ", "Russell", "Raymond"}; 

    return data; 

} 

回答

1

您可以使用data哈希參數傳遞給方法:

$.ajax({ 
    type: 'POST', 
    url: 'Default.aspx/GetCustomerNames', 
    data: '{ parameterName: "some test value" }', 
    contentType: 'application/json; charset=utf-8', 
    dataType: 'json', 
    success: function(msg) { 
     $("#example").autocomplete(msg.d); 
    }, 
    error: function(msg) { 
     alert("error"); 
    } 
}); 

和Web方式變爲:

public static string[] GetCustomerNames(string parameterName)