0

I'm using a formrequest file with laravel 5.2 to check input. This form i'm calling using jquery's $.post function. In my console, it's return 422 Unprocessable Entity which I suspect to be coming from the response since i'm not formatting it to json. One way of doing is this

I'd would like to know how I can invoke from my form request, the means to change the output messages to json.

Thanks!

UPDATE 1

JQuery looks like this:

$.ajaxSetup({
    headers: {
        'X-CSRF-TOKEN': $('input[name="csrf-token"]').val()
    }
});

$("#changePassword").on('click', function(e){
    e.preventDefault();

    var data = {};
    data.name = $("input[name='name']").val();
    data.surname = $("input[name='surname']").val();
    data._token = $('input[name="_token"]').val();

    $.post('url',data).done(function(data){
        $(".message").empty().html("Done!");
    }).fail(
        function(response, status){

        }
    );
});
1
  • Could you post the code for you $.post section? Commented Apr 27, 2016 at 7:29

1 Answer 1

2

Override the response method in your request class like below

    /**
     * Get the proper failed validation response for the request.
     *
     * @param  array  $errors
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function response(array $errors)
    {
        return Response::json([
            'error' => [
                'message' => $errors,
            ]
        ]);
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Right approach I ended up implementing it as: return response()->json([]);

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.