Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
add comment

2 Answers

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);
share|improve this answer
add comment

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 //#############################   
} 
share|improve this answer
 
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? –  Daniel Allen Langdon Apr 13 '11 at 16:18
 
When I use this code, I get the following error: "Invalid JSON primitive: "id" –  Daniel Allen Langdon Apr 13 '11 at 16:23
 
Check the link 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"] }; –  Daniel Allen Langdon Apr 13 '11 at 16:32
show 3 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.