【问题标题】:datatables returns Invalid json response when i am loading french text FROM database当我从数据库加载法语文本时,数据表返回无效的 json 响应
【发布时间】:2017-04-17 05:14:16
【问题描述】:

仅当我选择包含法语内容的列时才会收到此错误。可能是语言问题。我能做些什么来避免这个错误?

DataTables 警告:表 id=example - JSON 响应无效。有关此错误的更多信息,请参阅http://datatables.net/tn/1

$(document).ready(function() {
    $('#example').DataTable({
        "processing": true,
        "serverSide": true,
        /*"contentType": false,*/
        "ajax": '<?=base_url()?>'+"posts/get_html_posts"
    });
});
<table id="example" class=" ui table" cellspacing="0" width="100%">
    <thead>
        <tr>
            <th>No</th>
            <th>Category Name</th>
            <th>Subcategoty Name</th>
            <th>Title En</th>
            <th>Title Fr</th>
            <th>Is Draft</th>
            <th>Edit</th>
            <th>Delete</th>
            <th>Website url</th>
        </tr>
    </thead>
</table>

控制器

public function get_html_posts()
    {
        require(APPPATH .'third_party/ssp.class.php' );
        $table = 'posts';
        $primaryKey = 'post_id';
        $columns = array(
            array('db' => 'post_id','dt' => 0),
            array( 'db' => 'category_id', 'dt' => 1 ),
            array( 'db' => 'subcategory_id',  'dt' => 2 ),
            array('db'=>'title_en','dt'=>3),
            array('db'=>'title_fr','dt'=>4),
            array('db' => 'is_draft','dt' => 5,"formatter"=>function($d,$row) {
                if($row['is_draft']==1)
                return "<spna class='ui orange label'>Draft</span>";
                else
                return "<spna class='ui green label'>Published</span>";
            }),
            array('db' => 'post_id','dt'=>6,"formatter"=>function($d,$row) {
                return "<a href='".base_url()."posts/edit/".$row['post_id']."' class='ui orange button padding-10'><i class='write icon margin-0'></i></a>";
            }),

            array('db'=>'post_id','dt'=>7,"formatter"=>function($d,$row) {
                return "<a href='#' class='ui red button padding-10' onclick='return delete_record(this.id)' id='".$row['post_id']."'><i class='remove icon margin-0'></i></a>";
            }),
            array('db'=>'post_slug','dt'=>8,"formatter"=>function($d,$row)
            {
                $web_url = "http://localhost";
                return "<a class='ui orange button padding-10' target='_blank' href=".$web_url.'category/'.str_replace('-','', $row['category_id']).'/'.$row['subcategory_id'].'/'.$row['post_id'].'/'.$row['post_slug']."><i class='expand icon margin-0'></i></a>";
            })

        );

        $sql_details = array(
            'user' => 'root',
            'pass' => '',
            'db'   => 'my_demo',
            'host' => 'localhost'
        );
        //header('Content-Type: application/json');
        echo json_encode(
            SSP::simple( $_GET, $sql_details, $table, $primaryKey, $columns )
        );
    }

【问题讨论】:

  • 你也可以分享你的ajax输出吗?
  • 听起来您的 AJAX 响应的字符编码存在问题。请出示 PHP 代码。
  • 当我不得不处理法语时,我在使用单引号时遇到了一些问题,因为单引号经常出现在法语句子中。
  • @RoryMcCrossan 是的,我也认为。而且我没有在我的代码中使用任何字符编码。

标签: php jquery html mysql datatables


【解决方案1】:

我有时会遇到类似的问题。请确保您获得有效的 json 响应。 这是您可以参考所有与 jquery 数据表相关的查询的最佳示例。 Jquery Datatables

我的控制器看起来像这样:

public function inbox()
     {
         $data = array(
                    'receiverEmail' => $this->session->userdata['user_login']['loginEmail'],
            );
        $response = $this->mail_receive->inbox($data);

     $output = array(
                        "iTotalRecords" =>$this->mail_receive->totalRecords($data),
                        "iTotalDisplayRecords" => $this->mail_receive->totalRecords($data),
                         "aaData" => $response,
                );


        header('Content-Type: application/json');
        echo json_encode($output);

     }

这就是我进行 ajax 调用的地方

<script>
    $(document).ready(function() {
        var table = $('#inbox').dataTable( {


         "bServerSide": true,
         "sAjaxSource": "<?php echo base_url(); ?>index.php/Inbox_redirect/inbox",
         "bProcessing": true,
        "order": [],
         "bSortable" : true,
          "aaSorting": [[2, 'asc']],
         "sServerMethod": "GET",
         "columns" : [ 
            {"data" : "mailId"},
            {"data" : "mailSender"},
            {"data" : "mailSubject"},
            {"data" : "mailContent"},
            {"data" : "mailSendDate"} ],
        "columnDefs": [
        { 
            "targets": [ 0 ], //first column / numbering column
            "orderable": false, //set not orderable
        },
        ],

    } );
} );


</script>

请发布更多代码,以便更轻松地帮助您!

【讨论】:

  • 我已经使用示例datatables.net/examples/server_side/defer_loading.html实现了数据表
  • 在您的页面中,在适当的地方使用 utf8_encode() 以确保来自数据库或外部文件的值被正确编码(尝试将数据库中字段的编码也设置为 UTF8) ,看看 htmlentities() 函数将特殊字符解析为 html 实体,这也可以解决编码问题。
  • 我已经在包含法语文本的字段中使用了 utf8_encode(),但是我需要在哪里解码该内容?
  • utf8_decode() ...在您的控制器中使用它并传递解码的json字符串,因此ajax调用可以在处理数据后获得有效的json响应
猜你喜欢
  • 2023-03-12
  • 1970-01-01
  • 1970-01-01
  • 2016-09-06
  • 2017-09-23
  • 2021-11-09
  • 2021-11-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多