Kevin Kevin - 6 months ago 9
Javascript Question

How to post multiple sets of data as a single variable in an ajax request?

I currently have a jQuery ajax function which posts a multi-dimensional array to my server.

$.ajax({
type: "POST",
url: "Default.aspx/SaveQuoteProcesses",
data: "{'items':" + JSON.stringify(jaggedArray) + "}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg){
console.log('Success');
},
error: function (){
console.log('Fail');
}
});


Here is an example of the dataset I'm currently posting to the server:

[
{"QpcOpsID":"22","QpcQitID":"63"},
{"QpcOpsID":"20","QpcQitID":"63"},
{"QpcOpsID":"26","QpcQitID":"63"},
{"QpcOpsID":26,"QpcQitID":"63","QpcPprID":6,"PprQuestion":"How many colors?","AnswerValue":"4"}
]


I now need to send a regular array to the server in the same AJAX request.

["22", "20", "26"]


How can I include this new array in the existing javascript object?

Answer
var data = {
  items: jaggedArray,
  newArray: ["22", "20", "26"]
};

And then just JSON.stringify(data).