Search code examples
javascriptjquerylaraveldropzone

Dropzone with laravel, it displays 419 error


I have tested all that I have seen but it has not worked to me, I am using dropzone with laravel and my problem is that it displays a 419 error, I know what it means it's about csrf token but I can not fix it.

My HTML code is this:

<div id="my-dropzone" class="dropzone"></div>

My javascript code is this:

Dropzone.autoDiscover = false;

$(document).ready(function(){

   $('#my-dropzone').dropzone({
      url: 'http://54.161.128.196/pre_image/store',
      headers: {
        'X-CSRF-TOKEN': $('meta[name="token"]').attr('content')
      },
      method: 'post',
      dictResponseError: 'Error uploading file!'
   });

});

My Laravel route:

Route::post('/pre_image/store', 'Pre_ImageController@store');

I put this in the middleware to see if I could remove the 419 error:

It is in the VerifyCsrfToken middleware

protected $except = [
    'pre_image/store'
];

And it did not work, I have the csrf token in the meta like this:

<meta name="csrf-token" content="{{ csrf_token() }}">

This is screenshot of the error:

screenshot

So I wonder what is the problem why does it display 419 error if it that route is disabled to verify the token and even it has the token assigned?

Thanks!


Solution

  • I had similar issues with Dropzone. I ended up pulling the headers into an ajax function at the top of the page rather than in the DZ headers section. This allowed for any general ajax request to have the correct CSRF.

    Try this:

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