捕获并显示从$ http.post在angularjs中返回的laravel验证错误
问题描述:
我是laravel和laravel 5的新手,所以我有这样的困难时间,这只是问题后的问题,我目前的问题是这样的,我在我的前端使用角度js,所以我已经有客户端验证工作和实现,现在当我通过$ http.post发送我的表单时,表单也在服务器上验证,如果有验证错误,它将返回整个页面在响应,但我想只是作为json返回的错误,我目前正在尝试用户注册,而不是一个自定义的,但已经与laravel 5。
这是我register.js:
(function() {
angular.module('vendor').controller('RegisterController', ['$http', '$scope', function($http, $scope) {
$scope.d = {};
$scope.register = function() {
$http.post(
'/auth/register',
{
_token: $scope.d._token,
email: $scope.d.email,
password: $scope.d.password,
password_confirmation: $scope.d.password_confirmation
},
{
'X-Requested-With': 'XMLHttpRequest'
}
).success(function(data) {
// get errors here
});
};
}]);
})();
答
你会想,像这样捕捉和记录的错误:
$http.post(
'/auth/register',
{
_token: $scope.d._token,
email: $scope.d.email,
password: $scope.d.password,
password_confirmation: $scope.d.password_confirmation
},
{
'X-Requested-With': 'XMLHttpRequest'
}
).success(function(data) {
// successs
}).error(function(error) {
// error
console.log(error);
});