POST 请求 returns 422(无法处理的实体)
POST request returns 422 (unprocessable entity)
我有一个 AngularJS 应用程序,它使用 node.js 和 Loopbackjs 与 mysql 数据库进行通信。我能够在页面的 partial-home.html
方面正确显示我的 GET 请求,但是当我创建一个新用户时 returns 出现以下错误:
POST http://localhost:3000/api/se_users 422 (Unprocessable Entity)
这是我的代码:
部分-create.html
<div class="row">
<div class="col-md-8">
<h1>Create a User</h1>
</div>
</div>
<div class="container-fluid">
<div class="row">
<div class="col-md-6 col-md-offset-3">
<form name="create" ng-submit="se_user()">
<div class="form-group">
<label for="user_id">ID</label>
<input type="text" class="form-control" ng-model="user.eid" placeholder="Enter EID">
</div>
<div class="form-group">
<label for="first_nm">First Name</label>
<input type="text" class="form-control" ng-model="user.first_nm" placeholder="Enter First Name">
</div>
<div class="form-group">
<label for="last_nm">Last Name</label>
<input type="text" class="form-control" ng-model="user.last_nm" placeholder="Enter Last Name">
</div>
<div class="form-group">
<label for="email_adr">Email Address</label>
<input type="text" class="form-control" ng-model="user.email_adr" placeholder="Enter Item Type">
</div>
<div class="form-group">
<label for="user_role">Role</label>
<input type="text" class="form-control" ng-model="user.role" placeholder="Enter Status Code">
</div>
<div class="form-group">
<label for="user_active">Active</label>
<div class="radio">
<label>
<input type="radio" name="optionsRadios" id="optionsRadios1" value="Yes" ng-model="user.active" checked>Yes
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="optionsRadios" id="optionsRadios2" value="No" ng-model="user.active">No
</label>
</div>
</div>
<div class="form-group">
<label for="mgr_full_nm">Manager Name</label>
<input type="text" class="form-control" ng-model="user.mgr_full_nm" placeholder="Enter Full Name">
</div>
<div class="form-group">
<label for="mgr_eid">Manager EID</label>
<input type="text" class="form-control focus" ng-model="user.mgr_eid" placeholder="Enter Manager EID">
</div>
<div class="form-group">
<label for="insrt_dt">Insert Date</label>
<input type="date" class="form-control" ng-model="user.insrt_dt" placeholder="Enter Date">
</div>
<div class="form-group">
<label for="insrt_user_id">Insert User EID</label>
<input type="text" class="form-control" ng-model="user.insrt_user_id" placeholder="Enter User EID">
</div>
<div class="form-group">
<label for="upd_dt">Update Date</label>
<input type="date" class="form-control" ng-model="user.upd_dt" placeholder="Enter Update User Date">
</div>
<div class="form-group">
<label for="upd_user_id">Update User EID</label>
<input type="text" class="form-control" ng-model="user.upd_user_id" placeholder="Enter Update User EID">
</div>
<button class="btn btn-primary btn-lg" ng-click="addSe_user()">Create User</button>
<br>
<br>
<pre>
{{user | json}}
</pre>
</form>
</div>
</div>
</div>
app.js
angular.module('userApp', ['ui.router', 'lbServices'])
.controller('UserController', ['$scope', '$state', 'Se_user', function ($scope, $state, Se_user) {
$scope.users = [];
function getSe_users() {
Se_user
.find()
.$promise
.then(function (results) {
$scope.users = results;
});
}
getSe_users();
$scope.addSe_user = function () {
Se_user
.create($scope.user);
alert('You have created a new user!')
$scope.create.$setPristine();
};
$scope.editSe_user = function(user_id) {
Se_user
.editById(user_id)
.$promise
.then(function (user_id) {
getSe_users();
});
};
$scope.removeUser = function (item) {
Se_user
.deleteById(item)
.$promise
.then(function () {
getSe_users();
});
};
}])
.config(['$stateProvider', '$urlRouterProvider', function ($stateProvider, $urlRouterProvider) {
$urlRouterProvider.otherwise('');
$stateProvider
.state('home', {
url: '',
templateUrl: 'partial-home.html',
controller: 'UserController'
})
.state('create', {
url: '/create',
templateUrl: 'partial-create.html',
controller: 'UserController'
})
.state('admin', {
url: '/admin',
templateUrl: 'partial-admin.html',
contoller: 'UserController'
})
}]);
更改您的创建行
Se_user
.create($scope.user);
至
Se_user
.create($scope.user,
function(newUser) {
console.log(newUser);
// only set pristine here, this
// callback fires on success
$scope.create.$setPristine();
},
function(err) {
console.log(err); // will log the error to the console
}
);
警报不应妨碍查看开发工具输出,但这种结构使得通过在调用中使用错误回调更容易获得错误响应。
按照您的方式,表示您已创建新用户的警报将在 调用创建新用户的 ajax 之后立即触发 已发送—未收到。服务器响应会有一些延迟,因此您编写的任何依赖于正在创建(或出错)的新用户的代码都需要进入 .create()
方法的回调。
在此处查看 Loopback Angular $resource 文档:https://docs.strongloop.com/display/public/LB/AngularJS+JavaScript+SDK#AngularJSJavaScriptSDK-ModelresourceAPI
更新: Chrome 控制台 - 当您检查 Chrome 开发工具中的网络调用时,您应该会看到带有 422 错误的堆栈跟踪,例如此 401 错误:
请编辑您的原始问题,并post将该跟踪的全文作为代码块放入其中。
尝试更改 header 的内容类型:
header: { 'Content-Type' : 'application/x-www-form-urlencoded; charset=UTF-8;application/json' }
我有一个 AngularJS 应用程序,它使用 node.js 和 Loopbackjs 与 mysql 数据库进行通信。我能够在页面的 partial-home.html
方面正确显示我的 GET 请求,但是当我创建一个新用户时 returns 出现以下错误:
POST http://localhost:3000/api/se_users 422 (Unprocessable Entity)
这是我的代码:
部分-create.html
<div class="row">
<div class="col-md-8">
<h1>Create a User</h1>
</div>
</div>
<div class="container-fluid">
<div class="row">
<div class="col-md-6 col-md-offset-3">
<form name="create" ng-submit="se_user()">
<div class="form-group">
<label for="user_id">ID</label>
<input type="text" class="form-control" ng-model="user.eid" placeholder="Enter EID">
</div>
<div class="form-group">
<label for="first_nm">First Name</label>
<input type="text" class="form-control" ng-model="user.first_nm" placeholder="Enter First Name">
</div>
<div class="form-group">
<label for="last_nm">Last Name</label>
<input type="text" class="form-control" ng-model="user.last_nm" placeholder="Enter Last Name">
</div>
<div class="form-group">
<label for="email_adr">Email Address</label>
<input type="text" class="form-control" ng-model="user.email_adr" placeholder="Enter Item Type">
</div>
<div class="form-group">
<label for="user_role">Role</label>
<input type="text" class="form-control" ng-model="user.role" placeholder="Enter Status Code">
</div>
<div class="form-group">
<label for="user_active">Active</label>
<div class="radio">
<label>
<input type="radio" name="optionsRadios" id="optionsRadios1" value="Yes" ng-model="user.active" checked>Yes
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="optionsRadios" id="optionsRadios2" value="No" ng-model="user.active">No
</label>
</div>
</div>
<div class="form-group">
<label for="mgr_full_nm">Manager Name</label>
<input type="text" class="form-control" ng-model="user.mgr_full_nm" placeholder="Enter Full Name">
</div>
<div class="form-group">
<label for="mgr_eid">Manager EID</label>
<input type="text" class="form-control focus" ng-model="user.mgr_eid" placeholder="Enter Manager EID">
</div>
<div class="form-group">
<label for="insrt_dt">Insert Date</label>
<input type="date" class="form-control" ng-model="user.insrt_dt" placeholder="Enter Date">
</div>
<div class="form-group">
<label for="insrt_user_id">Insert User EID</label>
<input type="text" class="form-control" ng-model="user.insrt_user_id" placeholder="Enter User EID">
</div>
<div class="form-group">
<label for="upd_dt">Update Date</label>
<input type="date" class="form-control" ng-model="user.upd_dt" placeholder="Enter Update User Date">
</div>
<div class="form-group">
<label for="upd_user_id">Update User EID</label>
<input type="text" class="form-control" ng-model="user.upd_user_id" placeholder="Enter Update User EID">
</div>
<button class="btn btn-primary btn-lg" ng-click="addSe_user()">Create User</button>
<br>
<br>
<pre>
{{user | json}}
</pre>
</form>
</div>
</div>
</div>
app.js
angular.module('userApp', ['ui.router', 'lbServices'])
.controller('UserController', ['$scope', '$state', 'Se_user', function ($scope, $state, Se_user) {
$scope.users = [];
function getSe_users() {
Se_user
.find()
.$promise
.then(function (results) {
$scope.users = results;
});
}
getSe_users();
$scope.addSe_user = function () {
Se_user
.create($scope.user);
alert('You have created a new user!')
$scope.create.$setPristine();
};
$scope.editSe_user = function(user_id) {
Se_user
.editById(user_id)
.$promise
.then(function (user_id) {
getSe_users();
});
};
$scope.removeUser = function (item) {
Se_user
.deleteById(item)
.$promise
.then(function () {
getSe_users();
});
};
}])
.config(['$stateProvider', '$urlRouterProvider', function ($stateProvider, $urlRouterProvider) {
$urlRouterProvider.otherwise('');
$stateProvider
.state('home', {
url: '',
templateUrl: 'partial-home.html',
controller: 'UserController'
})
.state('create', {
url: '/create',
templateUrl: 'partial-create.html',
controller: 'UserController'
})
.state('admin', {
url: '/admin',
templateUrl: 'partial-admin.html',
contoller: 'UserController'
})
}]);
更改您的创建行
Se_user
.create($scope.user);
至
Se_user
.create($scope.user,
function(newUser) {
console.log(newUser);
// only set pristine here, this
// callback fires on success
$scope.create.$setPristine();
},
function(err) {
console.log(err); // will log the error to the console
}
);
警报不应妨碍查看开发工具输出,但这种结构使得通过在调用中使用错误回调更容易获得错误响应。
按照您的方式,表示您已创建新用户的警报将在 调用创建新用户的 ajax 之后立即触发 已发送—未收到。服务器响应会有一些延迟,因此您编写的任何依赖于正在创建(或出错)的新用户的代码都需要进入 .create()
方法的回调。
在此处查看 Loopback Angular $resource 文档:https://docs.strongloop.com/display/public/LB/AngularJS+JavaScript+SDK#AngularJSJavaScriptSDK-ModelresourceAPI
更新: Chrome 控制台 - 当您检查 Chrome 开发工具中的网络调用时,您应该会看到带有 422 错误的堆栈跟踪,例如此 401 错误:
请编辑您的原始问题,并post将该跟踪的全文作为代码块放入其中。
尝试更改 header 的内容类型:
header: { 'Content-Type' : 'application/x-www-form-urlencoded; charset=UTF-8;application/json' }