使用ajax将数组传递给webmethod

时间:2010-07-07 01:35:04

标签: jquery ajax

如何使用以下代码将数组传递给webmethod:

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

2 个答案:

答案 0 :(得分:2)

只是数组...

$.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"
});

你也可以对象......

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"
});

您可以查看$.ajax()的更多选项,其中包括data

答案 1 :(得分:-1)

我使用类似于以下方法的东西

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
});