Reputation: 468
I am creating an e-commerce web site with Nodejs/Sequelize and AngularJs.
I am actually got this starter AngularJS starter.
In my project I got 2 folders :
I would like to register a user, I got my "registeredView.html" with a form :
<form ng-submit="submit()">
<input type="text" name=firstname ng-model="user.firstname" placeholder="firstname" required=" ">
<input type="text" name="lastname" ng-model="user.lastname" placeholder="lastname" required=" ">
<input type="email" name="email" ng-model="user.email" placeholder="Email Address" required=" ">
<input type="password" name="password" ng-model="user.password" placeholder="Password" required=" ">
<input type="password" name="confirm_password" ng-model="user.confirm_password" placeholder="Password Confirmation" required=" ">
<input type="submit" value="Register">
</form>
For now, I could get those datas in my "RegisteredController" thanks to the $scope.user but I don't know how to send those data to my server.
I am using express module so I tried with $http.post like this :
.controller('RegisteredController', ['$scope', function ($scope, $http) {
$scope.submit = function () {
$http.post('/registered', $scope.user)
then(function (response) {
console.log("posted")
}).catch(function (response) {
console.error(response)
})
}
}]);
And on my "server.js" I tried to get this post like this :
app.get('/registered', function (req, res) {
console.log(req.body)
res.end();
});
But it dont works..
What I wrong ? Some Advice ? I need your helps guys please :)
Upvotes: 0
Views: 249
Reputation: 7188
As discussed in the comments there are 3 immediately apparent issues:
Your server is expecting a get
, not a post
, change app.get('/registered'...)
to app.post('/registered'...)
.
You're missing a dot here: $http.post('/registered', $scope.user)then(function (response) {
, between the closing )
and then
.
You need to mark your $http
to be injected, as with $scope
- change ['$scope', function ($scope, $http)
to ['$scope', '$http', function ($scope, $http)
.
To be able to do something with your posted data you should check out body parser. After requiring it you can do the following - app.use(bodyParser.json())
. Then you should have access to req.body
which should contain your user parsed as a javascript object.
Upvotes: 1