Reputation: 413
I've just managed to get my $http post working over Angular. It does what it needs to do, but when I post the data and then refresh the page it gives this error in my console:
Possibly unhandled rejection: {"data":null,"status":-1,"config":{"method":"POST","transformRequest":[null],"transformResponse":[null],"jsonpCallbackParam":"callback","url":"/insert","data":
This is my Angular post call:
app.controller('ContactCtrl', function($scope, $http){
$scope.formModel = {};
$scope.onSubmit = function(){
$http.post('/insert', $scope.formModel)
console.log('success');
};
});
And this is my server call in Express.js to my MongoDB:
router.post('/insert', function(req, res, next){
var item = {
name: req.body.name,
adress: req.body.adress,
postal: req.body.postal,
city: req.body.city,
email: req.body.email,
phone: req.body.phone,
quotation: req.body.quotation,
message: req.body.message
};
var data = new UserData(item);
data.save();
console.log('Item inserted');
});
Upvotes: 0
Views: 7342
Reputation: 54060
try with
var BACKEND_URL = "http://whatever:port";
var postData = angular.toJson($scope.formModel, true);
$http.post(BACKEND_URL + '/insert', postData);
and on the backend, either use res.sendStatus(200);
after mongo save operation or modify like below.
.save(function(err, result) {
if (err) throw err;
if(result) {
res.json(result)
}
})
Upvotes: 2