2

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

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

6 Comments

when i try the ur suggestion , this is what i get .... XMLHttpRequest cannot load 127.0.0.1:3000/…. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'localhost:9000' is therefore not allowed access. The response had HTTP status code 400.
In that case you should use CORS, more info is found here: html5rocks.com/en/tutorials/cors
on my server side here is how i handled CORS in my index.js file // CORS Support app.use(function(req, res, next) { res.header('Access-Control-Allow-Origin', '*'); res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE'); res.header('Access-Control-Allow-Headers', 'Content-Type'); next(); }); and even with that, i still have the above XMLHttpRequest error
can u help me out please
i can successfully do a GET from the server but i cant do a POST to the server
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.