When I selected a service from select input, I got a div created by JS as below:
// SOME CODE IN HERE.................................
Service 1
1 x 500
500
Service 2
1 x 1500
1500
SUBMIT
I create Ajax variable for using in Nodejs:
$("button[type='submit']").click(function (){
var length = $('.service__title').length;
var data = [];
alert($('.service__title:eq(0)').text());
alert(parseInt($('.display__number:eq(0)').text()));
for(let i = 0; i < length; i++){
data.push({
title: $('.service__title:eq(i)').text(),
quantity: parseInt($('.display__number:eq(i)').text()),
subtotal: parseInt($('.display__total:eq(i)').text())
})
}
$.ajax({
async:false,
url: "../booking/success",
type: "POST",
dataType: "html",
data: {data:data},
contentType: "application/x-www-form-urlencoded",
})
});
and console log:
app.post('/booking/success', async (req,res)=> {
console.log(req.body);
console.log(req.body.data);
res.render("success");
})
When I clicked Submit, my array "data" is undefined.
{ data:
[ { title: ' ', quantity: 'NaN', subtotal: 'NaN' } ]
[ { title: ' ', quantity: 'NaN', subtotal: 'NaN' } ]
Please help me!