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

Drewlim7's avatar

PATCH ajax request not receiving any data from using multipart form data

Hi all, I have setup all the form, ajax header, @csrf and @method('PATCH') under form tag correctly, however i could not receive any input data from the form and i only received an empty array after i set

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

in ajax header. I have debugged through web browser debugger (network tab), if i dont set the headers, then i will receive CSRF token mismatch. When i added the headers then received an empty array with the use of $request->all(). When i changed the ajax type to "POST", its working.... But i want it to be PATCH request.

I have asked a question through laracast before but no luck still not working. Here is my previous question : https://laracasts.com/discuss/channels/laravel/how-to-fix-csrf-token-mismatch-for-patch-ajax-request-2nd-time

and similar situation i found in stackoverflow but no answer also : https://stackoverflow.com/questions/40656248/cannot-read-multipart-form-data-from-request-in-laravel

0 likes
8 replies
jlrdw's avatar

I use jquery, and just a suggestion, try a put request, it's worked for me.

jlrdw's avatar

An answer I gave a while back

jquery

<script>
    $(function () {
        $("#postjq").click(function (event)
        {
            event.preventDefault();
            var $post = {};
            $post.petid = $('#petid').val();
            $post.species = $('#species').val();
            $post.ocheck = ($("#ocheck").prop("checked") == true ? '1' : '0');
            $post._token = document.getElementsByName("_token")[0].value
            $post._method = document.getElementsByName("_method")[0].value
            alert('here');
            $.ajax({
                url: 'http://localhost/laravel60/pet/petupdate',
                type: 'PUT',
                data: $post,
                cache: false,
                success: function (data) {
                    alert('Your data updated');
                    return data;
                },
                error: function () {
                    alert('error handing here');
                }
            });
        });
    });
</script>

route

Route::put('pet/petupdate', 'PetController@petUpdate');

I have these two lines also in form

<?php echo csrf_field(); ?>
<input type="hidden" name="_method" value="PUT">

controller

    public function petUpdate(Request $request)
    {

        $petid = $request->input('petid');
        $species = $request->input('species');
        //$ocheck = $request->input('ocheck');
        /* $postdata = [
          'species' => $species,
          'ocheck' => $ocheck
          ]; */
        $postdata = [
            'species' => $species
        ];
        //dd($postdata);
        DB::table('dc_pets')
                ->where('petid', $petid)
                ->update($postdata);
        
    }

Was done quick, no auth or validation, but add that.

And you can redirect from the jquery.

Your headers should work. Make sure you clear or delete temp views and clear browser cache between code changes.

mohansharma's avatar

Also add this Header to your headers

'Content-Type': 'application/json'

Like This

headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content'), 'Content-Type': 'application/json' }

Drewlim7's avatar

@jlrdw I have no problem by using form.serialize() method or your method. The problem occurs when i try to use data: new FormData($(form)[0]), because i need to upload image too.

mohansharma's avatar
Level 4

If you are using FormData then there is no need to include application/json header and you already specified @csrf in your form therefore FormData will also send that token so no need for X-CSRF-TOKEN header as well

And you need to send the request as POST and your laravel application will automatically get it as PATCH request because you included @method('PATCH') so your patch route and method for that route will get triggered

You could verify the method type in you controller method using this

$request->method()

Drewlim7's avatar

@mohansharma Ahhh i seee, no wonder why i set it to POST it will works. So if using FormData must set it as POST request?

Please or to participate in this conversation.