views:

497

answers:

1

I'm using the JQuery autocomplete plug in, and am passing in an array of strings to autocomplete (see code below). The method I'm calling to get my data (GetCustomerNames) is just returning an array of strings, and that is working fine. I need to find some way to pass in a parameter to the GetCustomerNames Method so I can filter what is being returned. Can anyone help with this?

Here is the markup code in the Default.aspx page:

<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>


Customer Name:

And here is the code in the Default.aspx.cs code behind page implementing the GetCustomerNames method:

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

    return data;

}
A: 

You could use the data hash to pass parameters to the method:

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

And your web method becomes:

public static string[] GetCustomerNames(string parameterName)
Darin Dimitrov