我正在处理一个项目,我遇到麻烦,从我的angularJS前端发布表单数据到使用restangular的后端RESTful API。每次我尝试我得到一个错误代码400(错误的请求)。下面是我的代码
app.js
'use strict';
angular
.module('clientApp', ['ngRoute', 'restangular'])
.config(function ($routeProvider, RestangularProvider) {
RestangularProvider.setBaseUrl('http://127.0.0.1:3000');
$routeProvider
.when('/', {
templateUrl: 'views/login.html',
controller: 'LoginCtrl',
controllerAs: 'login'
})
.when('/create/user', {
templateUrl: 'views/user-add.html',
controller: 'UserAddCtrl',
controllerAs: 'userAdd'
})
.otherwise({
redirectTo: '/'
});
});
查看/ user.html
<div ng-controller="UserAddCtrl">
<form class="form-horizontal" name="regForm" ng-submit="saveUser()">
<fieldset>
<div class="form-group">
<label class="col-md-4 control-label" for="textinput">First Name</label>
<div class="col-md-4">
<input id="Fname" name="Fname" type="text" ng-model="newUser.FnameValue" placeholder="Enter First Name" class="form-control input-md" required>
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label" for="Last Name">Last Name</label>
<div class="col-md-4">
<input id="Lname" name="Lname" ng-model="newUser.LnameValue" type="text" placeholder="Enter Last Name" class="form-control input-md">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label" for="Staff ID">Staff ID</label>
<div class="col-md-4">
<input id="StaffId" name="StaffId" ng-model="newUser.StaffIdValue" type="text" placeholder="Enter Staff ID" class="form-control input-md" required>
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label" for="Email">Email</label>
<div class="col-md-4">
<input id="Email" name="Email" ng-model="newUser.EmailValue" type="text" placeholder="Enter Email" class="form-control input-md" required="">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label" for="Password">Password</label>
<div class="col-md-4">
<input id="Password" name="Password" ng-model="newUser.PasswordValue" type="password" placeholder="Enter Password" class="form-control input-md" required>
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label" for="Register"></label>
<div class="col-md-4">
<button id="Register" name="Register" type="submit" class="btn btn-primary">Creat User</button>
</div>
</div>
</fieldset>
</form>
</div>
控制器/ user.js的
'use strict';
angular.module('clientApp')
.controller('UserAddCtrl', function ($scope, Restangular, $location) {
$scope.saveUser = function() {
$scope.newUser = {};
var restVar = Restangular.all('user');
restVar.post($scope.newUser).then(function() {
$location.path('/users');
console.log('Success');
}, function(response) {
console.log('Error with status code', response.status);
});
};
});
每次执行约代码时,我得到400(错误请求)......我做错了什么?
请注意我使用mongodb和我的数据库名称是isdbmeanapp,我的收藏名称是user,因此我的服务器网址是http://127.0.0.1:3000/user
答案 0 :(得分:2)
在Restangular帖子中,应该对集合而不是元素。
在你的情况下,POST应该像这样;
$scope.newUser = {email :"", password: "", etc: ""};
然后发出POST请求;
Restangular.all('user').post("users", $scope.newUser);
参考;