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;

}

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.