KoolReport's Forum

Official Support Area, Q&As, Discussions, Suggestions and Bug reports.
Forum's Guidelines

DataTable Server Side Error #1836

Closed Daniel opened this topic on on Jan 11, 2021 - 6 comments

Daniel commented on Jan 11, 2021

Hi, I'm having trouble getting DataTable to work with serverSide setting turned on. I'm using Laravel. Here's the code:

web.php

Route::get('/reports/{id}', [ReportController::class, 'show']);
Route::post('/reports/{id}', [ReportController::class, 'show']);

ReportController.php

$report = new MyReport($reportParams);
$report->run();
return view("report", ["report" => $report]);

report.blade.php

<?php $report->render(); ?>

MyReport.view.php

<?php
// use \koolreport\widgets\koolphp\Table;
use \koolreport\datagrid\DataTables;
use \koolreport\d3\BarChart;
use \koolreport\d3\ColumnChart;
use \koolreport\d3\PieChart;
use \koolreport\d3\DonutChart;
use \koolreport\d3\LineChart;
use \koolreport\d3\SplineChart;
use \koolreport\widgets\google\Histogram;
?>
<html>
  <head>
    <title>My Report</title>
    <meta name="csrf-token" content="<?php echo csrf_token(); ?>" />
    <script type="text/javascript" src="<?php echo asset('/js/jquery-3.5.1.min.js'); ?>" />
    <script type="text/javascript">
      $.ajaxSetup({
          headers: {
              'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
          }
      });
      </script> 
  </head>
  <body>
    <h1><center><?php echo $this->params['title']; ?></center></h1>
    <?php
      if ($this->params['visualization'] === 'table') {
        Datatables::create([
          'Title' => $this->params['title'],
          'dataSource' => function() {
            return $this->src($this->params['database'])->query($this->params['query_input']);
          },
          'columns' => $this->params['columns'],
          'options' => ['searching' => true, 'colReorder' => true, 'select' => true],
          'serverSide'=>true,
          'method'=>'post'
        ]);
      }
      elseif ($this->params['visualization'] === 'bar') {
        BarChart::create([
          'Title'=>$this->params['title'],
          'dataSource'=>$this->dataStore('default'),
          'columns'=>$this->params['columns']
        ]);
      }
      elseif ($this->params['visualization'] === 'column') {
        ColumnChart::create([
          'Title'=>$this->params['title'],
          'dataSource'=>$this->dataStore('default'),
          'columns'=>$this->params['columns']
        ]);
      }
      elseif ($this->params['visualization'] === 'pie') {
        PieChart::create([
          'Title'=>$this->params['title'],
          'dataSource'=>$this->dataStore('default'),
          'columns'=>$this->params['columns']
        ]);
      }
      elseif ($this->params['visualization'] === 'donut') {
        DonutChart::create([
          'Title'=>$this->params['title'],
          'dataSource'=>$this->dataStore('default'),
          'columns'=>$this->params['columns']
        ]);
      }
      elseif ($this->params['visualization'] === 'line') {
        LineChart::create([
          'Title'=>$this->params['title'],
          'dataSource'=>$this->dataStore('default'),
          'columns'=>$this->params['columns']
        ]);
      }
      elseif ($this->params['visualization'] === 'spline') {
        SplineChart::create([
          'Title'=>$this->params['title'],
          'dataSource'=>$this->dataStore('default'),
          'columns'=>$this->params['columns']
        ]);
      }
      elseif ($this->params['visualization'] === 'histogram') {
        Histogram::create([
          'Title'=>$this->params['title'],
          'dataSource'=>$this->dataStore('default'),
          'columns'=>$this->params['columns']
        ]);
      }
    ?>
  </body>
</html>

Here's the error:

I looked at the response and got 419 unknown status error mentioning CSRF token mismatch even though I already set the token in MyReport.view.php. Is there something that I'm missing?

Sebastian Morales commented on Jan 11, 2021

Daniel, after open your page pls check jQuery ajax setup by pasting the following command to your the browser console:

console.log($.ajaxSetup().headers);

Let us know its content as well as that of the meta tag's content attribute.

Daniel commented on Jan 12, 2021

Sebastian, I tried the log and the header is undefined. I changed

<script type="text/javascript" src="<?php echo asset('/js/jquery-3.5.1.min.js'); ?>" />

to

<script type="text/javascript" src="<?php echo asset('/js/jquery-3.5.1.min.js'); ?>"></script>

and the log returns correct value. However I got another error saying 'Invalid JSON response'. Below is the content of page:

<html>
  <head>
    <title>My Report</title>
    <meta name="csrf-token" content="MH1SxRjJMDQ7SeZjR2jGacv9y6M2YrJ1Bli5tvUy" />
    <script type="text/javascript" src="http://localhost/koolreport/public/js/jquery-3.5.1.min.js"></script>
    <script type="text/javascript">
      $.ajaxSetup({
        headers: {
          'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
      });
    </script>
  </head>
  <body>
    <script type='text/javascript' src='/koolreport/public/koolreport_assets/1096441198/KoolReport.js'></script><script type='text/javascript'>KoolReport.load.resources({"js":["\/koolreport\/public\/koolreport_assets\/2794630570\/jquery.min.js",["\/koolreport\/public\/koolreport_assets\/3529494954\/js\/bootstrap.min.js"]],"css":["\/koolreport\/public\/koolreport_assets\/3529494954\/css\/bootstrap.min.css","\/koolreport\/public\/koolreport_assets\/3529494954\/css\/bootstrap-theme.min.css"]});</script>
    <h1>
      <center>onedrive</center>
    </h1>
    <krwidget widget-name='datatables5ffcfd15d55e71' widget-type='koolreport/datagrid/DataTables'>
      <table id="datatables5ffcfd15d55e71" class='table display'>
        <thead>
          <tr>
            <th class=''>
              Name                    
            </th>
            <th class=''>
              Population                    
            </th>
            <th class=''>
              GNP                    
            </th>
          </tr>
        </thead>
      </table>
      <script type="text/javascript">
        KoolReport.widget.init(
            {"js":["\/koolreport\/public\/koolreport_assets\/2794630570\/jquery.min.js",["\/koolreport\/public\/koolreport_assets\/2656936528\/KRDataTables.js","\/koolreport\/public\/koolreport_assets\/2656936528\/datatables.min.js",["\/koolreport\/public\/koolreport_assets\/2656936528\/pagination\/input.js","\/koolreport\/public\/koolreport_assets\/2656936528\/AutoFill-2.3.5\/js\/dataTables.autoFill.min.js","\/koolreport\/public\/koolreport_assets\/2656936528\/ColReorder-1.5.2\/js\/dataTables.colReorder.min.js","\/koolreport\/public\/koolreport_assets\/2656936528\/RowGroup-1.1.2\/js\/dataTables.rowGroup.min.js","\/koolreport\/public\/koolreport_assets\/2656936528\/Select-1.3.1\/js\/dataTables.select.min.js"]]],"css":["\/koolreport\/public\/koolreport_assets\/2656936528\/KRDataTables.css","\/koolreport\/public\/koolreport_assets\/2656936528\/datatables.min.css"]},
            function() {
                
                var name = 'datatables5ffcfd15d55e71';
                var dtOptions = {"searching":true,"paging":false,"colReorder":true,"select":true,"serverSide":true,"ajax":{"url":"","data":function(d) {
                        d.id = 'datatables5ffcfd15d55e71';
                        d.scope = [];
                    },"type":"POST","dataFilter":function(data) {
                        var markStart = "<dt-ajax id='dt_datatables5ffcfd15d55e71'>";
                        var markEnd = '</dt-ajax>';
                        var start = data.indexOf(markStart);
                        var end = data.indexOf(markEnd);
                        var s = data.substring(start + markStart.length, end);
                        return s;
                    }},"columns":[{"data":"Name"},{"data":"Population"},{"data":"GNP"}],"columnDefs":[{"targets":0,"type":"string"},{"targets":1,"type":"number"},{"targets":2,"type":"number"}]};
                var fastRender = 0;
                if (fastRender) {
                    dtOptions.data = [{"Name":"Aruba","Population":"103,000","GNP":"828"}];
                }
                var dt = window[name] = $('#' + name).DataTable(dtOptions);
        
                var datatables5ffcfd15d55e71_data = {
                    id: 'datatables5ffcfd15d55e71',
                    searchOnEnter: 0,
                    searchMode: 'default',
                    serverSide: 1,
                    rowDetailData: dtOptions.rowDetailData,
                    showColumnKeys: ["Name","Population","GNP"],
                    fastRender: fastRender,
                    rowDetailIcon: 1,
                    rowDetailSelector: '',
                };
                KRdatatables5ffcfd15d55e71 = KoolReport.KRDataTables.create(
                    datatables5ffcfd15d55e71_data);
        
                
                        }
        );
      </script>
    </krwidget>
  </body>
</html>
Sebastian Morales commented on Jan 12, 2021

Pls check your ajax response (F12 -> Network tab -> XHR button) to see the json data inside the <dt-ajax...>...</<dt-ajax> tag. If it's not correct pls check your sql log to see if the sql query returns data.

Daniel commented on Jan 12, 2021

How do I get the response? I clicked XHR button and this showed up:

Sebastian Morales commented on Jan 12, 2021

It seemed I forgot one thing, please set an unique name for your DataTables in server side mode like this:

    DataTables::create(array(
        "name" => "myUniqueTableName",
        ...
    ));        

Tks!

Daniel commented on Jan 12, 2021

That solves the problem. Thanks for the help!

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

DataGrid