我正在将angularjs与nodejs集成,现在我在browser.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;
}
});
};
答案 0 :(得分:7)
根据维基百科的List of HTTP status codes:
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
的格式是否与服务器期望的格式相匹配。
您还应该检查响应正文以查看它是否包含更具体的错误。
答案 1 :(得分:0)
不知何故,我通过更改
解决了这个问题data: {}
到
params: {}