I'm building a front end Angular App, and a separate Laravel 5.1 API. I'm testing registration validation by POSTing duplicate e-mail addresses.
When it throws an error with status code,
return new JsonResponse($errors, 422);
my Angular $http method doesn't receive the response at all, and therefore success NOR error methods are called on the Angular response.
However, if I remove the status code 422 from JsonResponse:
return new JsonResponse($errors);
I can receive the response into Angular.
I find this odd because using the status code 422 still returns a response from the Network tab, but it's not received by Angular:
422 (Unprocessable Entity)
Response: email : ["The email has already been taken."]
I'd like to pass along the status code from Laravel because I want a non 200 to trigger the 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 was an error with your 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: I was also thinking it may be an issue with Angular error not being triggered by the 422 code. This says to add at the top but this did not fix the issue
$http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded";