KoolReport's Forum

Official Support Area, Q&As, Discussions, Suggestions and Bug reports.

CSRF token mismatch. #1356

Closed PT. Four Best Synergy opened this topic on on Mar 23 - 2 comments

PT. Four Best Synergy commented on Mar 23

I got 'CSRF token mismatch' error in console.

it works when i disabled the CSRF in laravel. but how to make it work when the CSRF enabled ?

this my route with post method

'Route::post('/sales/stock_free', 'StatusUnitSalesController@stock_free')->name('su_sales.stock_free');'

and this is my drilldown code

DrillDown::create(array(
        "name"=>"hondaDrillDown",
        "title"=>"Laporan Stock Free",
        "scope" =>  array(
            "_token" => csrf_token(),
        ),
        "levels"=>array(
            array(
                "title"=>"Tipe Mobil",
                "content"=>function($params,$scope)
                {
                    Table::create(array(
                        "dataSource"=>(
                            $this->src("pgsql")->query("
                                SELECT a.nama_tipe AS tipe, COUNT(a.qty) AS stok,
                                (COUNT(CASE WHEN a.status='STOCK FREE' THEN 1 END) - 
                                    (SELECT COUNT(CASE WHEN statusspk='SPK Taking' THEN 1 END) - 
                                    COUNT(CASE WHEN statusspk='SPK DO' THEN 1 END) 
                                    FROM data_spk ds
                                    WHERE ds.tipe = a.nama_tipe)) AS gap,
                                    (SELECT COUNT (CASE WHEN statusspk='SPK Taking' THEN 1 END) -
                                    COUNT(CASE WHEN statusspk='SPK DO' THEN 1 END) AS spk
                                    FROM data_spk WHERE tipe = a.nama_tipe) 
                                FROM inventory_stock_sales a
                                WHERE status='STOCK FREE'
                                GROUP BY tipe
                                ORDER BY tipe
                            ")
                        ),
                        "columns"=>array(
                            "tipe"=>array(
                                "type"=>"string",
                                "label"=>"Type"
                            ),
                            "stok"=>array(
                                "label"=>"Free Stock"
                            ),
                            "spk"=>array(
                                "label"=>"o/s SPK"
                            ),
                            "gap"=>array(
                                "label"=>"Gap"
                            ),

                        ),
                        "clientEvents"=>array(
                            "rowClick"=>"function(params){
                                hondaDrillDown.next({tipe:params.rowData[0]});
                            }",
                        )
                    ));
                }
            ),
            array(
                "title"=>function($params,$scope)
                {
                    return "Model ".$params["tipe"];
                },
                "content"=>function($params,$scope)
                {
                    Table::create(array(
                        "dataSource"=>(
                            $this->src("pgsql")->query("
                                SELECT nama_tipe as tipe, nama_warna as warna, max(status) as status, count(qty) as stok
                                FROM inventory_stock_sales 
                                WHERE status='STOCK FREE' and nama_tipe=:tipe
                                GROUP BY tipe, warna
                                ORDER BY tipe
                            ")
                            ->params(array(
                                ":tipe"=>$params["tipe"]
                            ))
                        )
                        ,
                        "columns"=>array(
                            "warna"=>array(
                                "type"=>"string",
                                "label"=>"Warna Mobil"
                                
                            ),
                            "stok"=>array(
                                "label"=>"Free Stock"
                            ),
                        ),
                        "clientEvents"=>array(
                            "itemSelect"=>"function(params){
                                hondaDrillDown.next({month:params.selectedRow[0]});
                            }",
                        )
                    ));
                }        
            ),
        ),
    ));
David Winterburn commented on Mar 23

Hi,

Please try removing "_token" in DrillDown's scope property and add the following line near the beginning of your page:

<meta name="csrf-token" content="<?php echo csrf_token(); ?>" />
<script type="text/javascript">
$.ajaxSetup({
    headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    }
});
</script> 

Let us know if this works out for you. Thanks!

PT. Four Best Synergy commented on Mar 24

ahhh ok thank you.

first i add the js code and meta element into the head. it still doesnt work. then i moved the jquery from the footer to the head. and poof it works. thanks once again

Build Your Excellent Data Report

Let KoolReport help you to make great reports. It's free & open-source released under MIT license.

Download KoolReport View demo
help needed
solved

DrillDown