I have a form and I am using Ajax to submit data to the API. The form contains a repeater which aids the user to enter any number of data repeatedly. The user submits the form after a few steps. When I try to bind the value to the Object is not binding. The API is built using Spring Boot, and by default, Jackson is used for conversion of JSON to Objects.
Here is the full code,
f.submit(function(e){
e.preventDefault();
var ignoreFields = ["_csrf"];
var unindexed_array = $(this).serializeArray().filter(val => ignoreFields.indexOf(val.name) === -1);
var indexed_array = {};
$.map(unindexed_array, function(n, i){
indexed_array[n['name']] = n['value'];
});
$.ajax({
type: $(this).attr('method'),
contentType: 'application/json',
url: $(this).attr('action'),
data: JSON.stringify(indexed_array),
dataType: 'json', cache: false, timeout: 600000,
success: function (d) {
}, error: function (e) {
}
});
})
before JSON.stringify()
{
act1[0].quantity: "4",
act1[0].name: "abc",
act1[0].status: "Working",
act2[0].quantity: "5",
act2[0].id: "1",
act3[0].quantity: "4",
act3[0].id: "2",
act3[0].unit: "mm",
activity: "zdzdsd zsdsad",
endTime: "23:02",
location: "sdasd",
note: "sdfsdfsd sdfsdfsd",
startTime: "03:03"
}
if I convert the above code to the following format I expect this to work, since I am using a list and Jackson Library will be able to identify this format
{
endTime: "23:02",
location: "sdasd",
note: "sdfsdfsd sdfsdfsd",
startTime: "03:03",
act1:[{
quantity: "4",
name: "abc",
status: "Working"
}],
act2:[{
quantity: "5"
id: "1"
}],
act3:[{
quantity: "4"
id: "2",
unit: "mm"
}]
}
I tried many ways in Javascript to convert into this format, How can we achieve the above format?
See Question&Answers more detail:
os