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.

1

2 Answers 2

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

Comments

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 //#############################   
} 

8 Comments

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?
When I use this code, I get the following error: "Invalid JSON primitive: "id"
Check the link jquery14.com/day-01/jquery-14#backwards for the traditional parameter need. ASP.Net
Did you remove the single quotes before and after the value being assigned to data?
var jdata = { "id": "bobby", "roles": ["at_admin", "at_user"] };
|

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.