javascript - 数据表: brake with url

标签 javascript php .htaccess datatables

我遇到了一个我不理解的数据表情况:

问题:

我有这个重写规则:

Rewriterule ^table/ table.php

所以,当我访问 www.site.com/table.php 时一切正常,但是当我访问 www.site.com/table/ 时,我有下一条警报消息:

DataTables warning: table id={tablet} - Invalid JSON response. For more information about this problem go to: http://datatables.net/tn/1

我访问了建议网址,但无法解决问题。

抱歉,我无法在线提供该网站,因为它是内部网。

我的PHP代码是这样的:

$table = 'paciente';
$primaryKey = 'id';
$columns = array(
    array( 
            'db' => 'id', 
            'dt' => 0),
    array( 
            'db' => 'nombre', 
            'dt' => 1,
            'formatter' => function( $d, $row ) {
                $links='<a href="paciente/'.$row[0].'/0/">'.$row[1].'</a>';
                return $links;
        }),
    array( 'db' => 'apellidos',  'dt' => 2 ),
    array( 'db' => 'tel_mov',   'dt' => 3,
     'formatter' => function( $d, $row ) {
         $tels=$row[3];
                return $tels;
         }
     ),
     array( 'db' => 'tel_casa',   'dt' => 4,
     'formatter' => function( $d, $row ) {
         $tels2=$row[4];
                return $tels2;
         }
     ),
    array( 'db' => 'terapeuta',     'dt' => 5 ),
    array( 
            'db' => 'id', 
            'dt' => 6,
            'formatter' => function( $d, $row ) {
                $links2='<a href="print-historia.php?height=655&amp;width=800&amp;id='.$row[0].'" class=" thickbox"><img src="ima/edit.png" /></a> | <a href="?borrar=total&amp;id='.$row[0].'" onClick="return confirmSubmit()"><img src="ima/close.png" /></a>';
                return $links2;
        }),
);
require( 'libraries/ssp.class.php' );

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

还有 JavaScript:

<script type="text/javascript" src="<?php echo $base; ?>js/datatables.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function() {
        $('#tablet').DataTable({
        "language": {
            "url": "js/Spanish.json"},
        "scrollY":        "500px",
        "scrollCollapse": true,
        "pageLength": 25,
        "processing": true,
        "serverSide": true,
        "ajax": "table-load.php",
        "order": [[ 1, "asc" ]],
        "aoColumns": [
        { "sTitle": "id","bVisible": false},
        { "sTitle": "Nombre" },
        { "sTitle": "Apellidos" },
        { "sTitle": "Móbil","bSortable": false },
        { "sTitle": "Fijo","bSortable": false },
        { "sTitle": "Terapeuta","bSortable": false },
        { "sTitle": "Edición","bSortable": false,"bSearchable": false }],
        "fnDrawCallback": function() {
        tb_init('a.thickbox');           
      }
        });
    } );
    </script>

有什么想法吗?提前致谢!!

最佳答案

我也有类似的问题。问题是 Datatable.js 需要从“data”数组键读取数据(老实说,在他们的文档中很难找到信息)。

这意味着您需要调整 PHP 脚本以获得具有以下结构的数组:

"success" => true|false // useful to display errors
"message" => string // explains the error (if occurred)
"data" => array // contains all your data

编辑

这是一个 PHP 数组的示例,已准备好由 DataTable.js 进行编码和处理。请注意数组键“data”:DataTable.js 将搜索要在该 jsonResponse.data 中显示的元素。

<?php

// data fetched from database
$fromDatabase = [
    'example',
    'test',
    'foo',
    'bar'
];

$response = [

    'success' => true,
    'message' => '',
    'data' => $fromDatabase
];

echo json_encode($response);
?>

关于javascript - 数据表: brake with url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33779963/

相关文章:

javascript - NodeJS : Deep copy a function

javascript - 获取缩放图像的原始大小

php - 在 PHP 中自动生成唯一 id,我可以指定 id 的起始编号

html - 确保万维网。或域名前的https

php - mod_rewrite 添加 .php 扩展

javascript - html @MEDIA print {},隐藏标记,在打印中留下空白

javascript - 为什么我的 Service Worker 总是在等待激活?

javascript - 如何编辑 woocommerce 产品页面?

php - PHP 中速记比较的动态计数

.htaccess - 同时使用 http 和 https