Click here to Skip to main content
15,884,353 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
In ajax method can I send multiple details array

My problem is I want to send more than one list details1, details2, details3,

But in ajax I have to combine all in one (data: JSON.stringify(DetailsAll),)

What I have tried:

JavaScript
$.ajax({
    type: "POST",
    url: "/Emp/MasterDetails",
    data: JSON.stringify(Details),
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    traditional: true,
    success: function (data) {
        alert(data.massge + " record(s) inserted.");
    }
});
Posted
Updated 6-Jun-20 20:44pm
v2
Comments
F-ES Sitecore 2-Jun-20 13:09pm    
You can send whatever you want, as long as the receiving method is configured to accept data in the format you are sending it.
Asif 7969814 3-Jun-20 10:50am    
i want to send like that
data: JSON.stringify(Details),JSON.stringify(Details2),JSON.stringify(Details3),
but its not working do you know what is right way.

1 solution

Yes! you can send multiple details array.

We can encapsulate the data like this:
$.ajax({
    type: "POST",
    url: "/Emp/MasterDetails",
    data: {'first_array':JSON.stringify(details1),
           'second_array':JSON.stringify(details2),
           'third_array':JSON.stringify(details3)},
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    traditional: true,
    success: function (data) {
        alert(data.massge + " record(s) inserted.");
    }
});

Your first_array, second_array, and third_array should be "arrays" at the receiving end. If not you can make it into an array.

For example:
$("#btnAjax").click(function() {
    var array1 = new Array();                
    $(".checkBoxList1:checked").each(function() {
         array1.push($(this).val());
     });
    var array2 = new Array();
    $(".checkBoxList2:checked").each(function() {
         array2.push($(this).val());
     });
var array3 = new Array();
    $(".checkBoxList3:checked").each(function() {
         array3.push($(this).val());
     });

    var Details = '{"first_array":["' + array1.join('","') + '"],"second_array":]"' + array2.join('","') + '"],"third_array":["' + array3.join('","') + '"]}';

//You can simply use this "Details" with data like this,
$.ajax({
    type: "POST",
    url: "/Emp/MasterDetails",
    data: Details,
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    traditional: true,
    success: function (data) {
        alert(data.massge + " record(s) inserted.");
    }
  });
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900