dimanche 4 septembre 2016

PHP Laravel : TokenMismatchException in VerifyCsrfToken.php line 67

Here the scenario is, I want to fetch some data from database and show it to user. While someone give some input and save it to database and I put a status with that which is always "0" until someone see it. So whenever a input has submitted user will see a pop up screen with that new data has inserted in another page. Here the problem is when ajax got some value with status "0" it shown it to screen but when it couldn't fetch any value ( when all the status value is 1)it shows error in console : TokenMismatchException in VerifyCsrfToken.php line 67. And confirm button came multiple time though it made the status 0 to 1 by click on it.

How do I solve the issue , any possible suggestion please ?

Here is the route:

Route::post('/unread',[
    'uses'=>'ItemController@getUnread',
    'as'=>'unread'
    ]);

Route::post('/s_update',[
    'uses'=>'ItemController@status_update',
    'as'=>'s_update'
    ]);

Here is the controller :

public function getUnread()
            {

                $items=DB::select(DB::raw("SELECT count(*) as total from items where status =0"));
                // $data=mysql_fetch_assoc($items);
                 return Response::json($items);
            }       

            public function status_update()
            {
                $items=DB::select(DB::raw("UPDATE items SET status=1 WHERE status=0"));
                return Response::json($items);
            }

and here is the ajax call :

  <script type="text/javascript">
                    setInterval(function(){ 
                            $(".container").load("list", function(){
                                $.ajaxSetup({
                                  headers: {
                                      'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                                      }
                                 });

                                  $.ajax({
                                    type : "POST",
                                    url : "",                                                             
                                     dataType : 'json',                             
                                    success : function(data) {

                                            $.each(data,function(index,subcatObj){                                                              
                                                if(subcatObj.total != '0')
                                                {
                                                    var yes = confirm('You have '+subcatObj.total+' new messages');
                                                            if(yes){
                                                                status_update();
                                                            }
                                                }

                                        });                             
                                    }, 
    });                                                                  
                        });          
                         }, 3000);  

                         function status_update(){

                           $.ajax({  
                                url:"",  
                                method:"POST",                                                                  
                           });  
                         }          
                </script>



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire