Search code examples
javascriptarraysjsonwebmethod

Passing an array to a web method in Javascript


I am currently trying to pass an array that I have created in Javascript to my webmethod in my aspx.cs.

Heres what I have:

JAVASCRIPT

function callServer(requestMethod, clientRequest) {


    var pageMethod = "Default.aspx/" + requestMethod;
    $.ajax({
        url: pageMethod,   // Current Page, Method  
        data: JSON.stringify({ request: clientRequest }), // parameter map as JSON  
        type: "POST", // data has to be POSTed  
        contentType: "application/json", // posting JSON content      
        dataType: "JSON",  // type of data is JSON (must be upper case!)  
        timeout: 60000,    // AJAX timeout  
        success: function (result) {
            ajaxCallback(result.d);
        },
        error: function (xhr, status) {
            alert(status + " - " + xhr.responseText);
        }
    });
}



function myButtonCalls()
{
var values=[];
values[0] = "Hello";
values[1] = "goodbye";

callServer("myMethod", values);
}

ASPX.CS

 [WebMethod]
        public static string myMethod(string[] request)
        {
return "This is test";
    }

It fails before it even gets to my web method. I know this code works for regualr strings but The ajax code that uses JSON doesnt see to want to work with arrays.

Any ideas of what i need to change?

Thanks


Solution

  • In the aspx.cs, I needed to accept with type list not array. Thanks for the comments!