I'm trying to send JSON data from a front-end form (angularJS) to the server (express/nodejs) then to MongoDB. However, I can't seem to send the data, when I log the data at the server end it gives me an empty data field (see pic below).
Client Front-end HTTP POST code
$http(
{
method: 'POST',
url: 'http://localhost:3456/todo',//Server URL
headers: {
'Content-Type': 'application/json',
'Accept':'application/json'
},
data: {item:'From Angular'}
}).then(function successCallback(response) {
console.log('Congrats, sent to Server');
console.log(response);
}, function errorCallback(response) {
console.log('Not sent to Server');
console.log(response);
});
Server end (POST request handler)
app.post('/todo',jsonParser,function(req,res){
var newTodo = Todo(req.body).save(function(err,data){
console.log(data);
console.log(req.body);
if(err) throw err;
res.json(data);
});
});
Client-end POST request (from angularjs)
How it should be
The problem is I can't seem to send the "data" to the server. On the server-side, it shows up empty as shown in Image 1. All the data ends up inside config -> data(as shown in 1) whereas the data field is empty.