Ajax 419 status error in laravel

2019-07-24 00:37发布

问题:

I tried to upload images using AJax,Jquery,Laravel. Here I tried with so many solutions still I am getting 419 error and in some cases getting 500 internal error.

Code I tried is

<form method="POST" id="needs" novalidate enctype="multipart/form-data">
            {{csrf_field()}}
                <input name="image1" id="image1" type="file" class="form-control" required="" />
                <br>
                <input type="file" name="image2" id="image2" class="form-control" required/>
                <br>
                <input type="file" name="image3" id="image3" class="form-control" required/>
                <br>
                <input type="file" name="image4" id="image4" class="form-control" required/>
                <br>
                <input type="file" name="image5" id="image5" class="form-control" required />
                <br>
                <button type="button" id="upload_image" name="upload_image" class="btn btn-lg btn-success" onclick="image_up();">Upload</button>
              </form>

Jquery:

function image_up()
{
  alert("Uploading start");
  $.ajax({
    headers: {
          'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
          },
        url: "{{route('collage.store')}}",
        type: 'POST',
        data : new FormData($(this)[0]),
        dataType: "json",
        cache : false,
    processData: false,
        success: function () {
          alert('form was submitted');
        }
      });
  }

Route:

Route::post('/', 'CollagePrimController@post')->name('collage.store');

Please anyone help me to fix this issue. Thanks in advance

回答1:

Laravel 419 status error is associated with token authorization only.

Add below code in your head section:

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

Add keep below code to your ajax call:

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

If still 419 error is coming then disable CSRF token from specific routes by modifying app/Http/Middleware/VerifyCsrfToken.php

class VerifyCsrfToken extends BaseVerifier
{

 // The URIs that should be excluded from CSRF verification.

    protected $except = [
    "/*"
    ];
}


回答2:

Please add folder permission if folder is already created. Else create folder and add 777 or 755 permission to that folder.