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

pag66's avatar
Level 5

token mismatch with csrf_field()

Hello everyone, I'm tusing the dropzone.js into my laravel aplication like this. https://laracasts.com/series/build-project-flyer-with-me/episodes/11 but when I try upload a file get the message : TokenMismatchException in VerifyCsrfToken.php line 67 here is my form

   <div class="col-md-9  main" >
                   @foreach($archivos as $archivo)
                   @endforeach
                <form action="/carpetas/{{$pac->id_pac}}/{{$directorio->nombre}}" class="dropzone" method="POST">
                    {{csrf_field() }}                     
                   </form> 
                </div>

Hello everyone, I'm tusing the dropzone.js into my laravel aplication like this. https://laracasts.com/series/build-project-flyer-with-me/episodes/11 but when I try upload a file get the message : TokenMismatchException in VerifyCsrfToken.php line 67 here is my form

   <div class="col-md-9  main" >
                   @foreach($archivos as $archivo)
                   @endforeach
                <form action="/carpetas/{{$pac->id_pac}}/{{$directorio->nombre}}" class="dropzone" method="POST">
                    {{csrf_field() }}                     
                   </form> 
                </div>

My controller is like this

public function subir(){
        return "working...";
    }

to make a previous ajax call i change VerifyCsrfToken like this :

protected function tokensMatch($request)
{
    // If request is an ajax request, then check to see if token matches token provider in
    // the header. This way, we can use CSRF protection in ajax requests also.
    $token = $request->ajax() ? $request->header('X-CSRF-Token') : $request->input('_token');

    return $request->session()->token() == $token;
}

What I'm making wrong please help me.

Greetings

0 likes
8 replies
usama.ashraf's avatar
{!! csrf_field() !!}

The rendered html has to be escaped.

pag66's avatar
Level 5

Update the code in this way:

<form action="/carpetas/{{$pac->id_pac}}/{{$directorio->nombre}}" class="dropzone" method="POST" enctype="multipart/form-data">
                    {{ csrf_field() }}
                     
                   </form> 

and

<form action="/carpetas/{{$pac->id_pac}}/{{$directorio->nombre}}" class="dropzone" method="POST" enctype="multipart/form-data">
                   {!! csrf_field() {!!
                     
                   </form> 

and have the same error. Thanks for the quickly reply.

pag66's avatar
Level 5

Also try the @Snapey solution, but have the same response of token mismatch, using ajax configuration

ahuggins's avatar

As @Snapey says, the {{ csrf_field() }} or {!! csrf_field() !!} won't matter if you are making an ajax request, those only get passed when the form is fully submitted.

You need to set the csrf_field somewhere, which you could grab from one of the above with whatever you are making the ajax call with (jquery or vue or whatever). Then you need to pass the token as the X-CSRF-Token header in your ajax request.

This looks like a pretty good example of how to do it: http://engageinteractive.co.uk/blog/csrf-protection-with-ajax-and-laravel

pag66's avatar
Level 5

Hello, thanks for yout replies, I'm using the @ajaygupta solution until can figure out what's going on with my form. thanks a lot

Greetings

Please or to participate in this conversation.