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

wassim.hattab's avatar

Getting TokenMismatchException in VerifyCsrfToken.php line 68 sometime

Hello,

I'm facing something weird in laravel while calling ajax function i got sometime token mismatch exception while i'm sending the token. So i did the javascript function in a way like i wait 7s to get a response if i don't have a response i add a retry button. When i click retry i got a response without Token mismatch error and sometimes from the first time i got the response.

Have anyone faced a problem like this?

0 likes
6 replies
samfrjn11's avatar

Can you post some code? What are you using to perform these requests? Where are you retrieving your token from?

fahad's avatar

hi, be sure to check if the csrf_token is being passed with the request, if you are using ajax request, it should be passed automatically by default in laravel.

wassim.hattab's avatar

Hello sorry for late reply this is my code:

HTML:

{{ Form::open(array('id'=>'login_form','class'=> 'panel p-a-4')) }}
            <label>
              <input type="email" name="email" placeholder="Email" />
            </label>
            <label>
              <input type="password" name="password" placeholder="Password" />
            </label>
            <label>
               Keep me logged in<input type="checkbox" checked />
            </label>
            <label>
              <button type="submit" id="signin_btn"><span>Login</span></button>
            </label>
{{ Form::close() }}

JAVASCRIPT:

$(document).ready(function(){
    $('#login_form').submit(function(ev){
        ev.preventDefault();
        $('#signin_btn > span').addClass('btn-loading');
        $('#login_errors').html('');
        $.ajax({
            type:"post",
            url:"{{url('/backend/login')}}",
            data:$('#login_form').serialize(),
            success:function(data){
                var result = JSON.parse(data);
                 $('#signin_btn > span').removeClass('btn-loading');
                if(result.result==0){
                    errors = "<ul>";
                    result.message.forEach(function(object) {
                        errors +='<li>'+object+'</li>'
                    })
                    errors += "</ul>";
                    show_response("error","Error",errors)
                }else{
                    if(result.response.type=='success'){
                        window.location.href = '{{url("backend/dashboard")}}';
                    }
                    else
                        show_response("error","Error","Incorrect email or password")
                }
            }
        }); 
    })  
})

PHP:

 public function login_submit(Request $request){
        $rules = array(
                'email'    => 'required|email',
                'password'    => 'required',
                
            );
       
           
        $validator = Validator::make(Input::all(), $rules);
        
        if ($validator->fails()) {
            $return['result']=0; 
            $return['message']=$validator->errors()->all(); 
            echo json_encode($return);
        } else {
             $credentials = array(
                'email'     => Input::get('email'),
                'password'  => Input::get('password')
                 );
            if (Auth::guard('admin')->attempt($credentials)) {
                $return['response']['type'] ='success'; 
                $return['response']['message']='Success!'; 
                echo json_encode($return);
            }else{
                $return['response']['type'] ='error'; 
                $return['response']['message']='Your username or password is incorrect'; 
                echo json_encode($return);

            }
        }
        
    }
shakti's avatar

use below code it might help


$(function(){

$('#login_form').on('submit',function(e){
    $.ajaxSetup({
        header:$('meta[name="_token"]').attr('content')
    })
    e.preventDefault(e);

        $.ajax({

        type:"POST",
        url:'/register',
        data:$(this).serialize(),
        dataType: 'json',
        success: function(data){
            console.log(data);
        },
        error: function(data){

        }
    })
    });
});
silverxjohn's avatar

Try this:

{{ Form::open(array('id'=>'login_form','class'=> 'panel p-a-4')) }}
    {{ csrf_field() }}
            <label>
              <input type="email" name="email" placeholder="Email" />
            </label>
            <label>
              <input type="password" name="password" placeholder="Password" />
            </label>
            <label>
               Keep me logged in<input type="checkbox" checked />
            </label>
            <label>
              <button type="submit" id="signin_btn"><span>Login</span></button>
            </label>
{{ Form::close() }}

Please or to participate in this conversation.