5

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;
        }
    });
};
4
  • you gotta post the code man we have nothing to go by. welcome to SO Commented Apr 6, 2016 at 5:09
  • 400 Bad Request is sent by the server because the request format is invalid. Commented Apr 6, 2016 at 5:12
  • You have a blank url, is that correct? Commented Apr 6, 2016 at 5:29
  • ya in that url we use Api url. Commented Apr 6, 2016 at 5:30

2 Answers 2

9

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.

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

5 Comments

data is valid because i got exact output of name,email,phone,address.now i got an error password field is missing.
@BalaDev I updated my answer with some more details. I hope that helps.
yes. now I got answer. its working perfectly. Thank u @ alex booker
@BalaDev Nice one. Please consider making my answer as accepted if it helped at all. Thanks.
Sorry about this, but I am facing a similar problem, in that I am getting an HTTP 400 when I post data using my ngResource service and get a proper response when I post the same request JSON through APIView in Django. Thanks in advance :)
0

Somehow, I solved this issue by changing

data: {}

to

params: {}

Comments

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.