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

salvador98's avatar

422 (Unprocessable Entity)

hi i use ajax for submit form. my ajax code is

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

$('#jform').on('submit' , function (e) { e.preventDefault(); data = $(this).serialize(); alert(data); url = $(this).attr('action');

    $.ajax({
   url : url,
   type : 'POST' ,
   data : data ,
   success:function (data) {
       toastr.success(data.success, 'پیام سیستم')
   } ,
   error: function(data) {
       var x=data.responseText;
       var jsonResponse = JSON.parse(x);

     if(jsonResponse.errors.phone)
     {
        msg(jsonResponse.errors.phone)
     }
  

   }


   });

});

and my controller code is

$valid = $request->validate([ 'name'=> 'string', ]);

    if($valid)
    {
        return response()->json(['success'=>'ok']);

    }

but i get 422 (Unprocessable Entity) error.. how fix it?

0 likes
8 replies
Cronix's avatar

422 is returned when there are validation errors.

Try this

$('#jform').on('submit' , function (e) {
    e.preventDefault();
    data = $(this).serialize();
    //alert(data);
    url = $(this).attr('action');

    $.ajax({
        url : url,
        type : 'POST' ,
        data : data ,
        dataType: 'json', // add this
        success:function (data) {
            toastr.success(data.success, 'پیام سیستم')
     } ,
    error: function(data) {
        console.log(data); // check in console for errors
    }
});

If you add dataType: 'json', it will return json. Then just check your console for validation errors.

Cronix's avatar

Force refresh your browser. What does "not resolved" mean? What did it do? Did it output the errors in your browsers javascript console? That's all it should be doing.

click's avatar

Take a look in your browser console and see what the server returns. I suppose it is saying something about a missing or empty name attribute?

what is the value of the js variable data? Could you add console.log(data) after you serialize it?

salvador98's avatar

my console log is

vendors.min.js:4 POST http://localhost:8000/Admin/User/Add/Submit 422 (Unprocessable Entity) send @ vendors.min.js:4 ajax @ vendors.min.js:4 (anonymous) @ Add:378 dispatch @ vendors.min.js:3 q.handle @ vendors.min.js:3 Add:388 {readyState: 4, getResponseHeader: ƒ, getAllResponseHeaders: ƒ, setRequestHeader: ƒ, overrideMimeType: ƒ, …}abort: ƒ (a)always: ƒ ()catch: ƒ (a)done: ƒ ()fail: ƒ ()getAllResponseHeaders: ƒ ()getResponseHeader: ƒ (a)overrideMimeType: ƒ (a)pipe: ƒ ()progress: ƒ ()promise: ƒ (a)readyState: 4responseJSON: {message: "The given data was invalid.", errors: {…}}responseText: "{"message":"The given data was invalid.","errors":{"name":["The name may not be greater than 2 characters."]}}"setRequestHeader: ƒ (a,b)state: ƒ ()status: 422statusCode: ƒ (a)statusText: "Unprocessable Entity"then: ƒ (b,d,e)proto: Object

Cronix's avatar
{message: "The given data was invalid.", errors: {…}}responseText: "{"message":"The given data was invalid.","errors":{"name":["The name may not be greater than 2 characters."

Yep, see, validation errors... The name may not be greater than 2 characters.

So you need to handle the errors and display them to the user, just like you would on a normal form page without ajax.

salvador98's avatar

no problem for show this msg.. i use alart and show error msg .. My problem is why the browser shows 422 (Unprocessable Entity) error

Cronix's avatar

Because you have validation errors. I don't know how to say it more clearly. If it ok, it will get a 200 response and do the action in the controller. If there is a validation error, it rejects the request and responds with 422 along with the validation error messages. Just like a normal form without ajax, except with ajax it doesn't redirect you back to the form. It just sends the errors back with a 422 response.

So, you display the validation errors to the user, they fix them, and resubmit the form like normal. I'm not sure what you're not understanding. See the little searchbox at the top of the screen? Type 422 into it. This has been asked and answered many, many times.

Please or to participate in this conversation.