2

How to pass an array to webmethod using the following code:

$.ajax({
    type: "POST",
    url: "somepage.aspx/somemethod",
    contentType: "application/json; charset=utf-8",
    dataType: "json"
});
1

2 Answers 2

2

just arrays...

$.ajax({
    type: "POST",
    url: "somepage.aspx/somemethod",
    data: "a[1]=1&a[2]=2&a[3]=3",
    contentType: "application/json; charset=utf-8",
    dataType: "json"
});

you might do also on objects...

var myObject = {
  a: {
    one: 1, 
    two: 2, 
    three: 3
  }, 
  b: [1,2,3]
};
$.ajax({
    type: "POST",
    url: "somepage.aspx/somemethod",
    data: decodeURIComponent($.param(myObject)), // a[one]=1&a[two]=2&a[three]=3&b[]=1&b[]=2&b[]=3
    contentType: "application/json; charset=utf-8",
    dataType: "json"
});

you can look at more options of $.ajax(), these includes data

Sign up to request clarification or add additional context in comments.

8 Comments

Good answer Reigel. OP, please see the data option: api.jquery.com/jQuery.ajax
the webmethod has the following signature but it is not getting called: [WebMethod] public static string somemethod(string[] arg) { }
what scripting language are you using? ASP.NET?
I have the js file which contains: var array = new Array(); $.ajax({ type: "POST", url: "somepage.aspx/somemethod", data: array contentType: "application/json; charset=utf-8", dataType: "json" }); and the method in the aspx.cs: [WebMethod] public static string somemethod(string[] arg) { }
Reigel - can u please show me the signature of your "somemethod" method would be?
|
-1

I use something similiar to the following method

var your_array = new array();
your_array[0] = 1;
your_array[1] = 2;

var data = { numbers: [] };

data.numbers = your_array;

$.ajax({
    type: "POST",
    url: "somepage.aspx/somemethod",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    data = json_data
});

Comments

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.