Natu Myers
Natu Myers

Reputation: 496

AngularJs Http Post not Posting

On button press, no action happens on the browser and there is no new user at my REST endpoint. registerUser() is triggered by button press. I closely followed a tutorial.

The html partial

<div class="section no-pad-bot" ng-controller="registerController" id="index-banner">

blah blah blah then

                <form>
                    <div class="row">
                        <div class=" input-field col s6 offset-s3">
                            <i class="material-icons prefix">account_circle</i>
                            <input type="text" class="validate" ng-model="user.username" placeholder="Username">
                        </div>
                    </div>
                    <div class="row">
                        <div class=" input-field col s6 offset-s3">
                            <i class="material-icons prefix">account_circle</i>
                            <input type="text" class="validate" ng-model="user.email" placeholder="Email">
                        </div>
                    </div>
                    <div class="row">
                        <div class=" input-field col s6 offset-s3">
                            <i class="material-icons prefix">account_lock</i>
                            <input type="text" class="validate" ng-model="user.password" placeholder="Password">
                        </div>
                    </div>
                    <button class="btn waves-effect waves-light" type="submit" ng-click="registerUser()">Submit
                                    <i class="material-icons right">send</i>
                                </button>
                </form>

In the clientapp.js

myApp.controller('registerController', function ($scope, $http) {
    $scope.registerUser = function () {
           // use $.param jQuery function to serialize data from JSON
            var data = $.param({
                hashword: $scope.password,
                username: $scope.username,
                email: $scope.email,
            });

            var config = {
                headers : {
                    'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8;'
                }
            }

            $http.post('/api/users', data, config)
            .success(function (data, status, headers, config) {
                console.log("successful post");
                $scope.PostDataResponse = data;
            })
            .error(function (data, status, header, config) {
                console.log("failed post");
                $scope.ResponseDetails = "Data: " + data +
                    "<hr />status: " + status +
                    "<hr />headers: " + header +
                    "<hr />config: " + config;
            });
        };

Upvotes: 0

Views: 52

Answers (1)

Mukesh Sharma
Mukesh Sharma

Reputation: 9022

Because in html page, you are referring user.username, 'user.email' and user.password instead of username, email and password respectivbely.

Try to change to following

<input type="text" class="validate" ng-model="username" placeholder="Username">
<input type="text" class="validate" ng-model="email" placeholder="Email">
<input type="text" class="validate" ng-model="password" placeholder="Password">

Hope it helps

Upvotes: 2

Related Questions