Is there a way I can ajax post an array of objects and have it parsed on a node.js server? Here is my client side code:
var context = [];
obj1 = {
first_name: 'e',
last_name: 'e',
contact_email: 'e',
contact_phone_num: 'e',
contact_notes: 'e'
}
obj2 = {
first_name: 'a',
last_name: 'a',
contact_email: 'a',
contact_phone_num: 'a',
contact_notes: 'a'
}
var context = [];
context.push(obj1);
context.push(obj2)
$.ajax({
type: "POST",
url: '/api/addcontact',
data: context,
success: function(data, status) {
alert('company added!');
},
error: function(data, status, res) {
console.log('err: ' + res);
}
});
My server side code:
api.post('/api/addcompany', function(req, res) {
console.log('add company hit');
console.log(req.body); //returns {}
res.sendStatus(200);
});
Right now, when I print the request body it returns {}.
Can someone help me access the array of objects properly on the server side?
Thanks in advance!