【问题标题】:Error: DataTables warning: table id=example - Requested unknown parameter using DataTables Server-side AJAX错误:DataTables 警告:table id=example - 使用 DataTables 服务器端 AJAX 请求未知参数
【发布时间】:2018-04-06 07:37:16
【问题描述】:

简介 我在做一个服务器端datatables.net json插件。

错误 一旦我尝试添加我的最后一列和第六列<th>Action,,我就会收到以下错误消息:

"DataTables 警告:表 id=example - 请求的未知参数 '6' 表示第 0 行第 6 列。有关此错误的更多信息,请 见http://datatables.net/tn/4"

我以前收到过此错误消息,并且知道它的一般含义。但是为什么它试图从数据库中提取数据呢?我从来没有用过$subdata[]=$row[6]; 我什至在这里看了一个视频:https://www.youtube.com/watch?v=tCB2BHCVSjs。此外,当您在错误消息上单击“确定”时,表格会完全正常加载(没有按钮除外)。 Index.php:

    <table id="example" class="display" style="width:100%">
        <thead>
            <tr>
                <th>ID</th>
                    <th>First name</th>
                    <th>Last name</th>
                    <th>Position</th>
                    <th>Date</th>
                    <th>Updated</th>
<th>Action</th>
                </tr>
            </thead>
    </table>

<?php
$records = mysqli_query($con, "SELECT * FROM employees");
$totalData= $records->num_rows;
$totalFiltered=$totalData;
$data=array();
while ($row = mysqli_fetch_array($records)) { 
    $subdata=array();
    $subdata[]=$row[0]; //id
    $subdata[]=$row[1]; 
    $subdata[]=$row[2]; 
    $subdata[]=$row[3]; 
    $subdata[]=$row[4]; 
    $subdata[]=$row[5]; 
      $subdata[]='<button></button>'; 
    $data[]=$subdata;
}

$requestData= $_REQUEST;

Server.php

 $json_data = array(
                      "draw" => intval(isset($_GET['draw'])), 
                      "recordsTotal"    => intval( $totalData ), 
                      "recordsFiltered" => intval( $totalFiltered ),
                      "data"            => $data //How To Retrieve This Data
                     );

    echo json_encode($json_data);  
    ?>

$table = 'employees';

// Table's primary key
$primaryKey = 'id';

$columns = array(
    array( 'db' => 'id', 'dt' => 0 ),
    array( 'db' => 'first_name', 'dt' => 1 ),
    array( 'db' => 'last_name',  'dt' => 2 ),
    array( 'db' => 'position',   'dt' => 3 ),
    array( 'db' => 'date',     'dt' => 4 ),
     array( 'db' => 'updated',     'dt' => 5 ),

);

// SQL server connection information
$sql_details = array(
    'user' => 'id3741634_username',
    'pass' => 'password',
    'db'   => 'id3741634_database',
    'host' => 'localhost'
);

require( 'ssp.class.php' );

echo json_encode(
    SSP::simple( $_GET, $sql_details, $table, $primaryKey, $columns )
);

【问题讨论】:

    标签: php json datatables


    【解决方案1】:

    试试这个

    $data=array();
    while ($row = mysqli_fetch_array($records)) { 
        $subdata['id']=$row['id']; //id
        $subdata['first']=$row['first']; //first
        $subdata['last']=$row['last']; //last
        $subdata['position']=$row['position']; //position
        $subdata['date']=$row['date']; //date
        $subdata['updated']=$row['updated']; //updated
        $subdata['button']='<button type="button" class="btn btn-warning">Warning</button>'; 
        $data[]=$subdata;
    }
    
    $requestData= $_REQUEST;
    $json_data = array(
                      "draw" => intval(isset($_GET['draw'])), 
                      "recordsTotal"    => intval( $totalData ), 
                      "recordsFiltered" => intval( $totalFiltered ),
                      "data"            => $data //How To Retrieve This Data
                     );
    
    json_encode($json_data);  
    ?>
    

    【讨论】:

    • 不认为它有效。但是我会在我拥有的早期基本版本上再试一次(理论上问题可能是别的,但你的代码可能是正确的)。
    • 很有趣。您的代码一直有效,直到您添加 HTML
    • @ScottSchmidt 问题修复了吗?
    • 不完全是。您的代码与我的代码完全相同,但错误相同。一旦添加了 操作 第 7 列,就会弹出错误消息。但是你的代码并没有让事情变得更糟(如果这有意义的话)
    • 我认为问题是计算机在 JSON 中看到 7 列,但在数据库中只有 6 列,所以一开始它会感到困惑。但随后意识到它没有向第 7 列发送任何数据。或者类似的东西......
    猜你喜欢
    相关资源
    最近更新 更多
    热门标签