2

I have the following ASP.net web method:

[WebMethod]
public static string SaveUserNew(string id, string[] roles)
{
 doStuff(id, roles);
}

I'm calling this code from jQuery Javascript code, but I don't know the syntax for passing an array. Ordinarily, I write jQuery code to call web methods that looks like this:

        $.ajax({
             type: "POST",
             url: "someUrl.aspx?webmethod",
             data: '{"foo":"fooValue"}',
             contentType: "application/json;",
             dataType: "json",
            }

Please shed some light on this.

Update: Here is an example of code without arrays that does work:

[WebMethod]
public static string SaveUserNew(string id)
{
    return "0";
}

        var jdata = '{ "id": "3TWR3"}';

        $.ajax({
            type: "POST",
            url: "UserMgmt.aspx/SaveUserNew",
            data: jdata,
            contentType: "application/json;",
            dataType: "json",
            traditional: true                 
            }
        });

My intention is to write some code in a similar style where I pass arrays to my web method.

Christian Strempfer
  • 7,291
  • 6
  • 50
  • 75
Vivian River
  • 31,198
  • 62
  • 198
  • 313
  • possible duplicate of [Passing array of strings to webmethod with variable number of arguments using jQuery AJAX](http://stackoverflow.com/questions/7971393/passing-array-of-strings-to-webmethod-with-variable-number-of-arguments-using-jq) – weir Jun 18 '14 at 17:17

2 Answers2

2

Passing param to webmethod is a little bit tricky. Try this one

[WebMethod]
public static string GetPrompt(string[] name)
{

    return "Hello " + name[0] + " and " + name[1];
}

jscript

var param = "{'name':['jack', 'jill']}";
var option = {
    error: function(request, status, error) {
        alert(error);
    },
    cache: false,
    success: function(data, status) {
        alert(data.d);
    },
    type: "POST",
    contentType: "application/json; charset=utf-8",
    data: param,
    dataType: "json",
    url: "../Server/ArrayParam.aspx/GetPrompt"
};

$.ajax(option);
Jules
  • 1,423
  • 13
  • 22
0

You need to
1) assign the data parameter to have an object with the properties id and roles.
2) assign the roles property an array of strings.
3) Set the traditional setting to true while passing options to ajax call.

e.g:

$.ajax({              
    type: "POST",              
    url: "someUrl.aspx?webmethod",              
    data: {
        "id":"1",
        "roles":[
            "Admin",
            "Something",
            "Another Thing"
        ]
    },
    contentType: "application/json;",              
    dataType: "json", 
    traditional: true //#############################   
} 
Chandu
  • 81,493
  • 19
  • 133
  • 134
  • I do have the array in my javascript code as an Array type. I wonder what is the easiest way of getting it into the data... And why did you add `traditional: true` to the code? – Vivian River Apr 13 '11 at 16:18
  • When I use this code, I get the following error: "Invalid JSON primitive: "id" – Vivian River Apr 13 '11 at 16:23
  • Check the link http://jquery14.com/day-01/jquery-14#backwards for the traditional parameter need. ASP.Net – Chandu Apr 13 '11 at 16:23
  • Did you remove the single quotes before and after the value being assigned to data? – Chandu Apr 13 '11 at 16:25
  • `var jdata = { "id": "bobby", "roles": ["at_admin", "at_user"] };` – Vivian River Apr 13 '11 at 16:32
  • If I add in the single-quotes, then I get a different error: `Error updating user: Internal Server Error - Type 'System.String' is not supported for deserialization of an array` – Vivian River Apr 13 '11 at 16:32
  • Are you are using the $.ajax submit a request to a SOAP Web Service? If that's the case its not gonna be that straight forward since you need to be submitting a soap request embedded in a HTTP Request and not a simple HTTP request – Chandu Apr 13 '11 at 16:36
  • What I am saying is that writing code like the sample I give in my question works fine as long as there are not arrays involved. The web methods are written in-line on my ASP.net pages. The pages were written this way by another developer and then handed off to me. – Vivian River Apr 13 '11 at 16:38