0
  1. I have added the code in the header view as shown below
<head>
<meta name="csrf-token" content="{{ csrf_token() }}">
</head>
  1. my ajax code as below
$.ajaxSetup({
          headers:
          {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
          }
 });
 $.ajax({
          method: "POST",
          url: "{{ route('ProgresOps.store') }}",
          data: form_data,contentType: false,       // The content type used when sending data to the server.
          cache: false,             // To unable request pages to be cached
          processData: false,
        })
        .done(function(data){
          toastr.success('Data saved.');
});

but when I submit the data the result is csrf mismatch, I haven't found a solution

exception: "Symfony\Component\HttpKernel\Exception\HttpException"
file: "C:\xampp74new\htdocs\swms\vendor\laravel\framework\src\Illuminate\Foundation\Exceptions\Handler.php"
line: 208
message: "CSRF token mismatch."

3 Answers3

0

There are so many ways simply try this (send csrf token with data directly)

data: {
        "_token": "{{ csrf_token() }}",
     }
0

There are 2 possibilities:

  1. The request from Ajax mismatch CSRF token parameter, resolve it by adding the CSRF token parameter to your request $.extend( query, { '_token' : 'your csrf token' });

  2. Mismatch XSRF-TOKEN in cookies. In my case I have resolved it by changing the config in config\session.php Change the secure config from true to false

Giangimgs
  • 952
  • 1
  • 12
  • 16
0

I had the same issue, ajax requests were working fine on local and not on server which was on https, despite passing token in header in ajax setup and tried all other solution but no luck, @Giangimgs 's answer gave me the hint, my culprit was the http_only value set to true in config/sessions.php, i changed it to false and all is working fine on live server. screenshot of value to be set to false

Nouman
  • 65
  • 1
  • 1
  • 6