doumi1912 2016-12-02 10:15
浏览 178
已采纳

当我加载法语文本FROM数据库时,datatables返回无效的json响应

I am getting this error only when I select a column which has French content in it. It may be a language issue. What can I do to avoid this error?

DataTables warning: table id=example - Invalid JSON response. For more information about this error, please see 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>

Controller

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 )
        );
    }
  • 写回答

1条回答 默认 最新

  • dongnao6858 2016-12-02 10:42
    关注

    I faced similar problem sometimes back.Make sure you get a valid json response. This is best example where you can refer for all jquery datatables related queries. Jquery Datatables

    My controller looked something like this:

    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);
    
         }
    

    And this is where I made the ajax call

    <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>
    

    Please post some more code so that it is easier to help you out!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 plotBAPC画图出错
  • ¥30 关于#opencv#的问题:使用大疆无人机拍摄水稻田间图像,拼接成tif图片,用什么方法可以识别并框选出水稻作物行
  • ¥15 Python卡尔曼滤波融合
  • ¥20 iOS绕地区网络检测
  • ¥15 python验证码滑块图像识别
  • ¥15 根据背景及设计要求撰写设计报告
  • ¥20 能提供一下思路或者代码吗
  • ¥15 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理