1

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";

1 Answer 1

1

The $http legacy promise methods success and error have been deprecated. Use the standard then method instead. If $httpProvider.useLegacyPromiseExtensions is set to false then these methods will 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 was an error with your login credentials. Please try again');
});
Sign up to request clarification or add additional context in comments.

2 Comments

Hmm odd, that seems to work. Not sure why deprecation would cause breaks though
@Growler are you using an $http interceptor? if so see stackoverflow.com/a/22425383/3574819

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.