Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

POST 400 bad request in angularjs

I am integrating an angularjs with nodejs now i got an error in browser.http 400 bad requset.I want to know is it front end error or back end error and i need solution.

register.html

      <form class="register-form" name="myRegisterForm" ng-hide="login" ng-submit="register(user)" novalidate>
             <input type="text" placeholder="Name" ng-model="user.name" name="username" minlength="5" maxlength="25"/>
        <span style="color:red" class="error" ng-show="myRegisterForm.username.$error.minlength">Given Name should be above 5 letters.</span><br/>
        <input type="text" placeholder="Phone number" ng-model="user.phone" name="phonenum"
               minlength="10" maxlength="10" ng-pattern="/^[0-9-]*$/" required/>
        <span style="color:red"  ng-style="{'padding-top':'2px'}" ng-show="myRegisterForm.phonenum.$error.pattern">Please enter numbers only.</span>
        <span style="color:red" class="error" ng-show="myRegisterForm.phonenum.$error.minlength">Phone number must be 10 digits .</span><br/>
        <input type="text" name=email placeholder="Email address" ng-model="user.email"
               ng-pattern="/^[a-z]+[a-z0-9._]+@[a-z]+\.[a-z.]{2,5}$/" required/>
        <span style="color:red" ng-style="{'padding-top':'2px'}" ng-show="myRegisterForm.email.$error.pattern">Please enter correct mail id</span><br/>
        <input type="password" placeholder="Password" ng-model="user.password" minlength="8" maxlength="16"/>
        <input type="password" placeholder="Confirm password" ng-model="user.cpassword" minlength="8" maxlength="16"/>
        <span style="color:red" ng-style="{'padding-top':'2px'}" ng-show="user.password !== user.cpassword">Password is not match</span>
        <textarea placeholder="Address" ng-model="user.address"></textarea>
        <input type="submit" value="SIGN UP" />
        <p class="message">Already registered? <p></form>

register.js

 $scope.register = function (user) {
    var data = {
        "user" : {
            "name": user.name,
            "email": user.email,
            "phone": "+91"+ user.phone,
            "password": user.password,
            "address": user.address

        }

    };
    $http({
        method: 'POST',
        url: '',
        headers: {'Content-Type': 'application/json'},
        data: data
    }).then(function successCallback(response) {
        if (response.data) {
            sharedServices.set(response.data);
            $location.path('/login');
        }
        console.log(response);
    }, function errorCallback(response) {
        if (response) {
            $scope.message = response.data;
        }
    });
};
like image 468
BalaDev Avatar asked Apr 06 '16 05:04

BalaDev


People also ask

What is 400 error in Angular?

Check for File Size As we've mentioned earlier, another cause for a 400 error is file size. If you're trying to upload a file that's too large that it exceeds the server file limit, you'll get a 400 error. To confirm that this is causing the issue, try to upload a file that's smaller in size.

What does 400 Bad request mean?

The HyperText Transfer Protocol (HTTP) 400 Bad Request response status code indicates that the server cannot or will not process the request due to something that is perceived to be a client error (for example, malformed request syntax, invalid request message framing, or deceptive request routing).


1 Answers

According to Wikipedia's List of HTTP status codes:

400 Bad Request

The server cannot or will not process the request due to an apparent client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing).[36]

In layman's terms, data is invalid:

var data = {
  "user" : {
    "name": user.name,
    "email": user.email,
    "phone": "+91"+ user.phone,
    "password": user.password,
    "address": user.address
  }
};

When the server tries to process data it detects that data is invalid and sends a 400.

Here are some possible causes:

  • data is missing a required value
  • data.phone is invalid
  • data.address is invalid
  • data.password is invalid (maybe it's too weak...?)
  • An important request header is missing

You should check that the format of data matches what the server expects.

You should also check the response body to see if it contains a more specific error.

like image 171
Alex Booker Avatar answered Sep 28 '22 04:09

Alex Booker