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

emnis's avatar
Level 1

CRSF Token mismatch error

Hello, I'am making a Single Page Application, after Two Ajax Requests I have this error " TokenMismatchException in VerifyCsrfToken.php line 67 ". How can I avoid that ? I notice that I'am starting with laravel 5.2. Thank you!

0 likes
3 replies
teodor_btc's avatar
Level 7

This is what i use

 // CSRF protection
    $.ajaxSetup({
        headers:
        {
            'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content')
        }
    });
emnis's avatar
Level 1

ok This is what i use too with angularjs :

$scope.update = function(){
                        $scope.loading = true;
                       $scope.record._token = $('input[name=_token]').val();
                       $http.put('/api/category/'+$scope.record.id,
                        $scope.record,
                        {'X-CSRF-Token': $('meta[name=_token]').attr('content')}
                                ).success(function(d) {
                                     $scope.loading = false;
                                  if(d.success){
                                     // window.location.href=d.url;
                                     //$scope.init();
                                  }else{
                                     // $scope.operation  = d;
                                  }
                             }).error(function(e){
                                 $scope.loading = false;
                                 console.log(e);
                                 //$scope.operation  = e;
                             });
                    };

is it correct ?

emnis's avatar
Level 1

Ok I saw my mistake, an a correct like this : In blade

App title ...

In the form view :

Libellé Description Enregistrer {{ csrf_field() }}

In Ajax Script :

$scope.update = function(){ $scope.loading = true; $scope.record._token = $('input[name=_token]').val(); $http.put('/api/category/'+$scope.record.id, $scope.record, {'X-CSRF-Token': $('meta[name=csrf-token]').attr('content')} ).success(function(d) { $scope.loading = false; if(d.success){ //window.location.href=d.url; //$scope.init(); }else{ // $scope.operation = d; } }).error(function(e){ $scope.loading = false; console.log(e); //$scope.operation = e; }); };

Now All is OK thank you very m atch!

Please or to participate in this conversation.