在Web方法中使用JSON数组获取数组?如何?

时间:2015-08-04 06:05:47

标签: javascript c# jquery arrays json

我有一个使用web方法的ajax函数而没有获取string []地址或任何其他数组字符串[] abc,在参数中,我的数组中的数组是

  MDate: "04-08-2015"
  MPurpose: 1
  MTime: "1010"
  RowNumber: 2
  address: Array[1]
  cell: Array[1]
  company: Array[1]
  designation: Array[1]
  id: "0"
  masterID: 0
  name: Array[1]
  nic: Array[1]

在JSON.STRINGIFY之后变为

  "{"name":["nouman","nouman"],"nic":["9089898","9089898"],"designation":  ["jkkhjk","jkkhjk"],"company":["uk","uk"],"cell":["+923012324265","+923012324265"],"address":["hkjhjk","hkjhjk"],"id":"0","MDate":"04-08-2015","MTime":"1010","MPurpose":1,"masterID":0,"RowNumber":3}"

他们是2个地址,单元格等为array []

我的ajax方法正确

  $.ajax({
             type: "POST",
             url: "AddNewMeeting.aspx/SetFileName",
             contentType: "application/json;charset=utf-8",
             data: JSON.stringify(Meeting),

             dataType: "json",
             success: function (data) {

                 alert(data);

             },
             error: function (result) {
                 //alert("Error login");

             }
         });
     }

我的网络方法只有当我使用其他参数,如地址,单元格等

时才有效
     [WebMethod]
    public static string SetFileName(string MDate, int MPurpose, string MTime, int RowNumber)
    {
        string c = "d";
        return c;
    }

我想收到

   //string[] address, string[] cell, string[] company, string[] designation, int id, int masterID, string[] name, string[] nic)

也在参数中,但它不起作用

2 个答案:

答案 0 :(得分:0)

我们可以像这样发送数组

  

companyArray = [' Value1',' Value2']
  结果= {MDate:" 04-08-2015",公司:companyArray}
  返回JSON.stringify(结果)

Web服务将在哪里

  

public string webService(string MDate,string [] companies)

它对我有用

答案 1 :(得分:0)

您需要将traditional属性添加到您的AJAX请求中,将其设置为true并移除JSON.stringify

var meetingData = {
    MDate: "04-08-2015",
    MPurpose: 1,
    MTime: "1010",
    RowNumber: 2,
    address: [ 'string1', 'string2' ],
    cell: [ 'string1', 'string2' ],
    company: [ 'string1', 'string2' ],
    designation: [ 'string1', 'string2' ],
    id: "0",
    masterID: 0,
    name: [ 'string1', 'string2' ],
    nic: [ 'string1', 'string2' ],
};

$.ajax({
    type: "POST",
    url: "AddNewMeeting.aspx/SetFileName",
    contentType: "application/json;charset=utf-8",
    data: meetingData,
    dataType: "json",
    traditional: true,
    success: function (data) {
        alert(data);
    },
    error: function (result) {
        alert("Error login");
    }
});