Javascript 在 angularjs 中 POST 400 错误请求

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36442007/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 19:04:48  来源:igfitidea点击:

POST 400 bad request in angularjs

javascriptangularjs

提问by BalaDev

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.

我正在将 angularjs 与 nodejs 集成,现在我在 browser.http 400 bad requset 中出现错误。我想知道是前端错误还是后端错误,我需要解决方案。

register.html

注册.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

注册.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;
        }
    });
};

回答by Alex Booker

According to Wikipedia's List of HTTP status codes:

根据维基百科的HTTP 状态代码列表

400 Bad Request

The servercannot 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]

400 错误请求

服务器不能或不会对请求处理由于表观客户端错误(例如,恶意请求语法,无效请求消息成帧或欺骗性请求路由选择)。[36]

In layman's terms, datais invalid:

通俗地说,data是无效的:

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 datait detects that datais invalid and sends a 400.

当服务器尝试处理时,data它检测到data无效并发送400.

Here are some possiblecauses:

以下是一些可能的原因:

  • datais missing a required value
  • data.phoneis invalid
  • data.addressis invalid
  • data.passwordis invalid (maybe it's too weak...?)
  • An important request headeris missing
  • data缺少必需的值
  • data.phone是无效的
  • data.address是无效的
  • data.password无效(也许它太弱了......?)
  • header缺少一个重要的请求

You should check that the format of datamatches what the server expects.

您应该检查格式是否data与服务器期望的匹配。

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

您还应该检查响应正文以查看它是否包含更具体的错误。

回答by Jeffrey Roosendaal

Somehow, I solved this issue by changing

不知何故,我通过改变解决了这个问题

data: {}

to

params: {}