简体   繁体   中英

Posting form data from AngularJs frontend to NodeJS RESTful API using restangular

im working on a project and im having troubles posting form data from my angularJS frontend to my backend RESTful API using restangular. each time i try i get a error code 400(bad request). heres my code below

app.js

'use strict';

angular
    .module('clientApp', ['ngRoute', 'restangular'])
    .config(function ($routeProvider, RestangularProvider) {

    RestangularProvider.setBaseUrl('http://127.0.0.1:3000');

    $routeProvider
      .when('/', {
      templateUrl: 'views/login.html',
      controller: 'LoginCtrl',
      controllerAs: 'login'
    })

      .when('/create/user', {
      templateUrl: 'views/user-add.html',
      controller: 'UserAddCtrl',
      controllerAs: 'userAdd'
    })
     .otherwise({
      redirectTo: '/'
    });
    });

view/user.html

<div ng-controller="UserAddCtrl">

    <form class="form-horizontal" name="regForm" ng-submit="saveUser()">  
        <fieldset>

            <div class="form-group">
                  <label class="col-md-4 control-label" for="textinput">First Name</label>  
                  <div class="col-md-4">
                  <input id="Fname" name="Fname" type="text" ng-model="newUser.FnameValue" placeholder="Enter First Name" class="form-control input-md" required>

                  </div>
            </div>


            <div class="form-group">
                  <label class="col-md-4 control-label" for="Last Name">Last Name</label>  
                  <div class="col-md-4">
                  <input id="Lname" name="Lname" ng-model="newUser.LnameValue" type="text" placeholder="Enter Last Name" class="form-control input-md">

                  </div>
            </div>


            <div class="form-group">
                  <label class="col-md-4 control-label" for="Staff ID">Staff ID</label>  
                  <div class="col-md-4">
                  <input id="StaffId" name="StaffId" ng-model="newUser.StaffIdValue" type="text" placeholder="Enter Staff ID" class="form-control input-md" required>                   
                  </div>
            </div>


            <div class="form-group">
                  <label class="col-md-4 control-label" for="Email">Email</label>  
                  <div class="col-md-4">
                  <input id="Email" name="Email" ng-model="newUser.EmailValue" type="text" placeholder="Enter Email" class="form-control input-md" required="">

                  </div>
            </div>


            <div class="form-group">
                  <label class="col-md-4 control-label" for="Password">Password</label>
                  <div class="col-md-4">
                    <input id="Password" name="Password" ng-model="newUser.PasswordValue" type="password" placeholder="Enter Password" class="form-control input-md" required>

                  </div>
            </div>

            <div class="form-group">
                  <label class="col-md-4 control-label" for="Register"></label>
                  <div class="col-md-4">
                    <button id="Register" name="Register" type="submit" class="btn btn-primary">Creat User</button>
                  </div>
            </div>

        </fieldset>
    </form>

</div>

controller/user.js

'use strict';

angular.module('clientApp')
  .controller('UserAddCtrl', function ($scope, Restangular, $location) {

     $scope.saveUser = function() {
         $scope.newUser = {};

         var restVar = Restangular.all('user');

         restVar.post($scope.newUser).then(function() {
             $location.path('/users');
             console.log('Success');
         }, function(response) {
             console.log('Error with status code', response.status);
         }); 
     };

 });

each time i execute the about codes, i get 400(bad request)... what im i doing wrong?

please note that im using mongodb and my db name is isdbmeanapp and my collection name is user, hence my server url is http://127.0.0.1:3000/user

In Restangular posts should be done to collections not elements.

In your case POST should be made like this;

$scope.newUser = {email :"", password: "", etc: ""};

Then make POST request like;

Restangular.all('user').post("users", $scope.newUser);

Reference;

https://github.com/mgonto/restangular#element-methods

Restangular POST form data in json format in Angular JS

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM