nCore nCore - 4 months ago 13
Javascript Question

angularjs http post method

Trying to post something back to the mongodb however its not sending anything, when I click submit its passing {} back to the cmd and the network console is hanging on pending then it will fail when its taking long to post.

Can someone shed a light on this one please, thanks.

Html:

<input type="text" ng-model="vm.user">
<input type="text" ng-model="vm.pass">


Service:

function _postUser(user,pass){
var params = {
user: user,
pass:pass
}
return $http({
method: 'POST',
url: '/loginDB',
params: params
})
}


Get users: I get the users from the DB.

vm.getUsers = function (){
homeService.getUsers()
.then(function(response){
vm.users = response.data;
console.log(vm.users);
});
}


Post action:

vm.postUser = function() {
// console.log('send it back')
homeService.postUser(vm.user)
.then(function(response){
console.log('send it back')
})
}


Server.js app.post back to db

app.post('/loginDB', function (req, res){
console.log(req.body);
});


Edit: its posting but now taking the ng-model, I know something is wrong with the ng-model but just can't get my head on it.

db.loginDB.insert(req.body, function(err, doc){
res.json(doc);
})

Answer

Try changing params key to data

 return $http({
        method: 'POST',
        url: '/loginDB',
        data: params
    })

In the html too you have the same model for both the inputs,

controller declaration,

vm.newuser = {user:'',pass:''};

html

<input type="text" ng-model="vm.newuser.user">
<input type="text" ng-model="vm.newuser.pass">
Comments