1

I'm using Laravel 5.2 and i'm using Laravel's Form Post Validation method. It is validating as expected. But if validation fails it should return the error message as JSON since i'm sending an ajax request. But it is returning the previous page as html. My request is looks like : enter image description here

as per Laravel Docs in https://laravel.com/docs/5.2/validation#working-with-error-messages it shoud return a json. Is there anything i missed ?

2
  • 1
    Check your headers, try "Accept": "application/json" or dataType : 'json' Commented Aug 13, 2016 at 13:22
  • Its Works with your suggestion. Thanks ! Commented Aug 13, 2016 at 13:23

1 Answer 1

1

When validation fails, the response is 422 HTTP status code. In ajax you need get it in error: section.

It'll be like following:

$.ajax({
    url: url,
    type: "POST",
    data: formData,
    success: function(data) {
        // Process if there is not error
    },
    error: function(data){
        // Error in json object. Process with errors.
    }
});
Sign up to request clarification or add additional context in comments.

2 Comments

i have handled the error as you said . but it is returning 200 response. that is the reason i'm posting this question. it should return 422
Hi its working as i added setRequestHeader('Accept', 'application/json, text/javascript')

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.