我正在将 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/