Search code examples
jqueryasp.netweb-servicesasmx

asp.net : how to send a array from client to server web method


In ASP.NET, I have collected a list of selected items from a checkbox in an array at the client side. Now I need to pass the array from client to server's ASMX web method. How do I do this?


Solution

  • Add a JSON library to your page, use json2.js here. That gives you a function to serialize javascript arrays into JSON strings.

    You can then pass it into your webmethod:

    [WebMethod] 
    public void MyWebMethod(List<string> someValues)
    {
        // Use someValues...
    }
    

    Here's the javascript you need

    var arrayData = ["1","2","3"]; // Your array goes here
    
    $.ajax({
      type: "POST",
      url: "MyWebService.asmx/MyWebMethod",
      data: JSON.stringify({ someValues: arrayData }),
      contentType: "application/json; charset=utf-8",
      dataType: "json",
      success: function() 
      {
        // Your success function...
      }
    });