Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

kazehaya's avatar

422 (Unprocessable Entity error when submitting form with ajax

het there im trying to send a form with ajax but im getting this error.

422 (Unprocessable Entity)

    <script>
        $(function() {

            $( "#productForm" ).submit(function( e ) {

                e.preventDefault();


                var token = $('input[name=_token]').val();
                var dataString = $(this).serialize();

                $.ajax({
                    type: "POST",
                    url: "contact-product",
                    data: {_token: token, data: dataString },
                    success: function (form) {
                        $("#contact-success").modal('show');
                    }
                });
            });
        });
    </script>

This is my datastring:

_method=PUT&_token=GfEc7e07jT4chnDUHFJzXFK7viLQAZ4n3neeZ13d&firstname=John&email=johndoe%40mail.com&lastname=Doe&phone=425345345&questions=no+info&custom=&product=testproduct+R+4024+TC
jquery.js:8625 POST http://example.local/contact-product 422 (Unprocessable Entity)

Here is the response in my network tap which i think is causing the trouble:

{"firstname":["The firstname field is required."],"lastname":["The lastname field is required."],"email":["The email field is required."]}
0 likes
12 replies
JarekTkaczyk's avatar
Level 53

@kazehaya 422 is natural response code for invalid data and the errors say exactly the same. Next thing, you're passing {_token: "...", data: "serialized data"}, so you don't have firstname, lastname, 'email` keys in your submited data.

Andreyco's avatar

Assuming that _token is part of submited form, use this snippet

$( "#productForm" ).submit(function( e ) {

    e.preventDefault();

    $.ajax({
        type: "POST",
        url: "contact-product",
        data: $(this).serializeArray(),
    contentType: "application/json",
        success: function (form) {
            $("#contact-success").modal('show');
        }
    });
});
kazehaya's avatar

$(this).serializeArray() wouldn't work already tried that :/

Andreyco's avatar

Why so? Works for me all the time... also, specify contentType. JSON maybe...?

kazehaya's avatar

It has something to do with:

    public function rules()
    {
        return [
            'firstname' => 'required',
            'lastname'  => 'required',
            'email'     => 'required'
        ];
    }

If i comment the required fields out it works.. any idea why?

Andreyco's avatar

Of course, since they are not required, validation passes, lol.

Looks like data don't reach server. Have you tried dumping data before writing this post?

kazehaya's avatar

yeah i tried that see datastring in my post, thats what im trying to send to my method.

1 like
kazehaya's avatar

@JarekTkaczyk was right i needed to pass the keys first, i created a var for each field and then send it with ajax. See example below. So many thanks @JarekTkaczyk

var firstname = $('input[name=firstname]').val();
var lastname = $('input[name=lastname]').val();

ajax part:

data: {_token: token, firstname: firstname, lastname: lastname}
1 like
apueee's avatar

I was getting same error when i tried to get the form html with error messages after validation.

I solved my problem this way

$validation = Validator::make($request->all(), $validationRules);

if ($validation->fails())
{
       return  Redirect::route('users.create')->withInput()->withErrors($v->errors());
 }

Now i can use same form for ajax and non ajax request :)

2 likes
tuckbloor's avatar

Hi, Laravel 5.8 i found the answer the answer

https://stackoverflow.com/questions/32757586/error-422-ajax-post-using-laravel

     error :function( data ) {
        if( data.status === 422 ) {
            var errors = $.parseJSON(data.responseText);
            $.each(errors, function (key, value) {
                // console.log(key+ " " +value);
            $('#response').addClass("alert alert-danger");

                if($.isPlainObject(value)) {
                    $.each(value, function (key, value) {                       
                        console.log(key+ " " +value);
                    $('#response').show().append(value+"<br/>");

                    });
                }else{
                $('#response').show().append(value+"<br/>"); //this is my div with messages
                }
            });
          }

Please or to participate in this conversation.