javascript - angularjs中的POST 400错误请求

标签 javascript angularjs

我正在将 angularjs 与 nodejs 集成,现在我在浏览器中遇到错误。http 400 错误请求。我想知道是前端错误还是后端错误,我需要解决方案。

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

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

最佳答案

根据维基百科的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]

通俗地说,data是无效的:

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

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

以下是一些可能的原因:

  • data 缺少必需的值
  • data.phone 无效
  • data.address 无效
  • data.password 无效(可能是太弱了...?)
  • 缺少一个重要的请求 header

您应该检查data 的格式是否符合服务器的要求。

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

关于javascript - angularjs中的POST 400错误请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36442007/

相关文章:

javascript - Django 日期字段 JavaScript

php - 如何通过 AJAX 脚本检查 HTML 文档中包含的文件是否发生更改?

javascript - 从外部指令访问表单验证

javascript - SuiteScript 清空字段值中的 record.setFieldValues() 问题

javascript - Angularjs 中的 ngRouting 问题

javascript - 使用 ng-repeat 生成差异 View

javascript - 如何使用纯 JavaScript 实现此按钮行为?

javascript - 如何展开 - 在 ui 网格分组中折叠行单击?

javascript - 以 angular 形式追加为 html

javascript - Angular 2 Electron 项目编译出现问题