最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - POST 400 bad request in angularjs - Stack Overflow

programmeradmin3浏览0评论

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;
        }
    });
};

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;
        }
    });
};
Share Improve this question edited Apr 6, 2016 at 5:17 BalaDev asked Apr 6, 2016 at 5:09 BalaDevBalaDev 2011 gold badge3 silver badges13 bronze badges 4
  • you gotta post the code man we have nothing to go by. wele to SO – omarjmh Commented Apr 6, 2016 at 5:09
  • 400 Bad Request is sent by the server because the request format is invalid. – Poul Bak Commented Apr 6, 2016 at 5:12
  • You have a blank url, is that correct? – Poul Bak Commented Apr 6, 2016 at 5:29
  • ya in that url we use Api url. – BalaDev Commented Apr 6, 2016 at 5:30
Add a ment  | 

2 Answers 2

Reset to default 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.

Somehow, I solved this issue by changing

data: {}

to

params: {}
发布评论

评论列表(0)

  1. 暂无评论