I'm using AJAX to post JSON:
app.use(express.json());
app.use(bodyParser.urlencoded({extended:true}))
app.use(express.urlencoded({ extended: true}));
const rowObject=JSON.stringify(rowData)
$.ajax({
type: "POST",
url: '/api/entities/liquid',
data: rowObject,
dataType: 'application/json',
success: function(){
//success code here
},
error: function(){
//error code here
}
});
return rowObject
When I console.log(req.body), it returns the following:
{
'{"key1":"value1","key2":"value2"...}': ''
}
I should be able to extract value1 by using req.body.value1 but it's always undefined. The client side shows a payload of [{"key1":"value1","key2":"value2"}]: in an Array[0] I'm getting a 400 response but I know that's because I'm not able to extract the values. I've tried req.query, req.params and also tried to add the index of the array like req.body[0].key1 - still undefined. I'm a novice so this may be just a basic js issue but I don't understand why extracting the value is so complicated. I have another form that works fine using the data[0].value1 format. I tried using "data" as well and it's not recognized.
If You Are Using express.json() middleware than just use req.body first console it and check it you are getting the the data or not.
PS: Please Provide More Info About question. And if you are using express version of 4 than you dont need to download body parser