当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - get array with in JSON array in web method? How to?

问题描述:

i have a working ajax function with web method without getting string[] address or any other array string [] abc , in parameter , My arrays within array is

 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]

after JSON.STRINGIFY it becomes

 "{"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}"

their are 2 adresses , cell etc as array[]

my ajax method correct

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

}

});

}

my web method works only when i use parameters other then arrays like adress , cell etc

 [WebMethod]

public static string SetFileName(string MDate, int MPurpose, string MTime, int RowNumber)

{

string c = "d";

return c;

}

i want to recieve

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

also in parameter but how it would not work

网友答案:

We can send arrays as parameter like this

companyArray = ['Value1', 'Value2']
result = { MDate: "04-08-2015", companies: companyArray }
return JSON.stringify(result)

Where web service will be

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

It works for me

网友答案:

You need to add the traditional property to your AJAX request, set it to true and remove the 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");
    }
});
分享给朋友:
您可能感兴趣的文章:
随机阅读: