0

Everything was working fine. No issue. But suddenly from nowhere this error started to show up.

"CSRF token mismatch.",

All forms was working fine and next day I open project and found this error. Very strange, how a working code started to show error all of the sudden...!!

Here is my code

 $.ajax({
    type:'POST',
    url:"{{ route('loginuser.post') }}",
    data:{_token: "{{ csrf_token() }}",email:email,password:password,remember:remember},
    dataType:'json',
    success:function(data){

As you can see here is I am adding csrf token in this line

data:{_token: "{{ csrf_token() }}",email:email,password:password,remember:remember},

I try to clear cache too but still didn't work. How a working code can do like this all of the sudden. Laravel is surely a strange framework!!

Rishabh
  • 610
  • 2
  • 11
  • 32
  • https://stackoverflow.com/questions/32738763/laravel-csrf-token-mismatch-for-ajax-post-request – Ankita Dobariya Aug 23 '21 at 04:34
  • maybe it's because your "data" json object is broken. – N69S Aug 23 '21 at 04:35
  • @AnkitaDobariya Solution given in your linked post, I have already used in my code. See my post, second code line, here I mentioned. – Rishabh Aug 23 '21 at 04:35
  • @N69S How is this? What can I do about it? – Rishabh Aug 23 '21 at 04:36
  • Did you try to clear the sessions in addition to the cache? – apokryfos Aug 23 '21 at 04:44
  • replace it with `{"_token": "{{ csrf_token() }}","email":email,"password":password,"remember":remember}` – N69S Aug 23 '21 at 04:45
  • @apokryfos Yes I did, using php artisan commands in powerShell. – Rishabh Aug 23 '21 at 04:47
  • @N69S STill same error – Rishabh Aug 23 '21 at 04:48
  • Try it in incognito as well or in a different browser. If it's still not working then check if sessions are created successfully on the server – apokryfos Aug 23 '21 at 04:50
  • @apokryfos Nothing works. But when I try to move project from local to live server then it worked. I mean, this issue only coming in localhost. – Rishabh Aug 23 '21 at 05:52
  • Is a session being created in localhost? What is your session driver? – apokryfos Aug 23 '21 at 08:18
  • @apokryfos Session is not created. I can't go to login restricted page. – Rishabh Aug 23 '21 at 11:13
  • If session is not created then (if using a file session driver) check if the www user has permission to write to the sessions folder (usually storage/framework/sessions) or if you're using a different driver take troubleshooting steps based on that – apokryfos Aug 23 '21 at 11:30
  • @apokryfos I found the solution. It seems cache problem (don't know how). But when I try to run form in other browser then it started to work. What exactly cause it, still a mystery to me – Rishabh Aug 26 '21 at 11:03

0 Answers0