我正在将angularjs与nodejs集成在一起,现在在浏览器中出现错误.http 400错误的requset。我想知道是前端错误还是后端错误,我需要解决方案。
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;
}
});
};
根据Wikipedia的HTTP状态代码列表:
400错误的要求
该服务器不能或不会对请求处理由于表观客户端错误(例如,恶意请求语法,无效请求消息成帧或欺骗性请求路由选择)。[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
与服务器期望匹配的格式。
您还应该检查响应主体,以查看其是否包含更具体的错误。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句