php - Passing status code from Laravel not received by Angular http -
i'm building front end angular app, , separate laravel 5.1 api. i'm testing registration validation posting duplicate e-mail addresses.
when throws error status code,
return new jsonresponse($errors, 422);
my angular $http
method doesn't receive response @ all, , therefore success
nor error
methods called on angular response.
however, if remove status code 422
jsonresponse
:
return new jsonresponse($errors);
i can receive response angular.
i find odd because using status code 422
still returns response network tab, it's not received angular:
422 (unprocessable entity)
response: email : ["the email has been taken."]
i'd pass along status code laravel because want non 200 trigger angular $http .error
response.
code snippets:
angular:
signup: function(params) { return $http({ method: 'post', url: url + ver + '/auth/register', data: params, cache: true }); } auth.signup(scope.user).success(function(res){ console.log(res); }).error(function(err) { alertify.error('there error login credentials. please try again'); });
laravel:
public function postregister(request $request) { $validator = $this->validator($request->all()); if ($validator->fails()) { $this->throwvalidationexception( $request, $validator ); } protected function buildfailedvalidationresponse(request $request, array $errors) { if ($request->ajax() || $request->wantsjson()) { return new jsonresponse($errors, 422); }
edit: thinking may issue angular error
not being triggered 422
code. this says add @ top did not fix issue
$http.defaults.headers.post["content-type"] = "application/x-www-form-urlencoded";
the $http legacy promise methods success , error have been deprecated. use standard method instead. if $httpprovider.uselegacypromiseextensions set false these methods throw $http/legacy error.
https://docs.angularjs.org/api/ng/service/$http
try using then
instead:
auth.signup(scope.user).then(function(res) { console.log(res); }, function(err) { alertify.error('there error login credentials. please try again'); });