Invalid JSON response when adding some utf-8 characters

Invalid JSON response when adding some utf-8 characters

trinqtrinq Posts: 3Questions: 0Answers: 0

Hi,
I have some problem with DataTables 1.10 and server-side script. Everything works well when I import to MySQL table CSV data without German and Polish characters, but when I add some of this specific characters, I instantly receive an Invalid JSON response error. All of settings in phpmyadmin database is set to utf-8 as well as main php file. I would be gratefull for any help.

Replies

  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin

    What is the server sending back that makes it invalid JSON? See tech note 1 to find out how to get this information. In future, please also follow the forum rules and link to a test case showing the problem.

    Allan

  • trinqtrinq Posts: 3Questions: 0Answers: 0

    Hi Allan, thanks for reply. The thing is, when I check XHR tab it shows no data at all, only "This request has no response data available" When I clear MySQL table and import the same CSV data, but with manualy deleted special characters, all works like a charm.

  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin

    "This request has no response data available"

    That's not valid JSON :-). It sounds like there is an error in the server-side script. Have you got error logging enabled and have you checked the error logs?

    Allan

  • trinqtrinq Posts: 3Questions: 0Answers: 0

    Yes, I have and it show nothing. I post my php files:

    index.php:

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="initial-scale=1.0, maximum-scale=2.0">
    
        <title></title>
        <link rel="stylesheet" type="text/css" href="css/jquery.dataTables.css">
        <link rel="stylesheet" type="text/css" href="css/shCore.css">
        <link rel="stylesheet" type="text/css" href="css/demo.css">
        <style type="text/css" class="init">
    
        th, td { white-space: nowrap; }
        div.dataTables_wrapper {
            width: 800px;
            margin: 0 auto;
        }
    
        </style>
        <script type="text/javascript" language="javascript" src="js/jquery.js"></script>
        <script type="text/javascript" language="javascript" src="js/jquery.dataTables.js" charset="utf-8"></script>
        <script type="text/javascript" language="javascript" src="js/shCore.js"></script>
        <script type="text/javascript" language="javascript" src="js/demo.js"></script>
        <script type="text/javascript" language="javascript" class="init" charset="utf-8">
    
    $(document).ready(function() {
        $('#datatables').dataTable( {
            "processing": true,
            "serverSide": true,
            "ajax": "server_processing.php"
        } );
    } );
    
        </script>
    </head>
    
    <body class="dt-example">
        <div class="container">
                <table id="datatables" class="display" cellspacing="0" width="100%">
                    <thead>
                    <tr>
                        <th>Name</th>
                        <th>Date 1</th>
                        <th>Date 2</th>
                        <th>Place</th>
                        <th>Book number</th>
                        <th>Location</th>
                        <th>Status</th>
                        <th>Country</th>
                        <th>Religion</th>
                        <th>Card ID</th>
                        <th>Description</th>
                        <th>Data 1</th>
                        <th>Data 2</th>
                    </tr>
                     </thead>
                
                </table>
        </div>
        
    </body>
    </html>
    

    server_processing.php:

    <?php
     
    /*
     * DataTables example server-side processing script.
     *
     * Please note that this script is intentionally extremely simply to show how
     * server-side processing can be implemented, and probably shouldn't be used as
     * the basis for a large complex system. It is suitable for simple use cases as
     * for learning.
     *
     * See http://datatables.net/usage/server-side for full details on the server-
     * side processing requirements of DataTables.
     *
     * @license MIT - http://datatables.net/license_mit
     */
     
    /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
     * Easy set variables
     */
     
    // DB table to use
    $table = 'datatables';
     
    // Table's primary key
    $primaryKey = 'id';
     
    // Array of database columns which should be read and sent back to DataTables.
    // The `db` parameter represents the column name in the database, while the `dt`
    // parameter represents the DataTables column identifier. In this case simple
    // indexes
    $columns = array(
        array( 'db' => 'name', 'dt' => 0 ),
        array( 'db' => 'date_1',  'dt' => 1 ),
        array( 'db' => 'date_2',   'dt' => 2 ),
        array( 'db' => 'place',     'dt' => 3 ),
        array( 'db' => 'number',     'dt' => 4 ),
        array( 'db' => 'location',     'dt' => 5 ),
        array( 'db' => 'status',     'dt' => 6 ),
        array( 'db' => 'country',     'dt' => 7 ),
        array( 'db' => 'religion',     'dt' => 8 ),
        array( 'db' => card',     'dt' => 9 ),
        array( 'db' => descr',     'dt' => 10 ),
        array( 'db' => 'data_1',     'dt' => 11 ),
        array( 'db' => 'data_2',     'dt' => 12 ),
    );
     
    // SQL server connection information
    $sql_details = array(
        'user' => 'root',
        'pass' => 'urel',
        'db'   => 'datatables',
        'host' => 'localhost'
    );
     
     
    /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
     * If you just want to use the basic configuration for DataTables with PHP
     * server-side, there is no need to edit below this line.
     */
     
    require( 'ssp.class.php' );
     
    echo json_encode(
        SSP::simple( $_GET, $sql_details, $table, $primaryKey, $columns )
    );
    
  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin
    edited May 2014

    With no errors, there is not much to go on I'm afraid. In fact, I can't even begin to guess what is going wrong. I'd suggest that you add some debugging statements into the code - I suspect that there is an error occurring and it isn't showing up.

    Allan

  • julicrackjulicrack Posts: 1Questions: 0Answers: 0
    edited September 2014

    Hi!

    I solve it with this change in spp.class.php. Only add htmlentitites()

    Juli.

    static function data_output ( $columns, $data )
    {
    $out = array();

        for ( $i=0, $ien=count($data) ; $i<$ien ; $i++ ) {
            $row = array();
    
            for ( $j=0, $jen=count($columns) ; $j<$jen ; $j++ ) {
                $column = $columns[$j];
    
                // Is there a formatter?
                if ( isset( $column['formatter'] ) ) {
                    $row[ $column['dt'] ] = htmlentities($column['formatter']( $data[$i][ $column['db'] ], $data[$i] ));
                }
                else {
                    $row[ $column['dt'] ] = htmlentities($data[$i][ $columns[$j]['db'] ]);
                }
            }
    
            $out[] = $row;
        }
    
    
    
        return $out;
    }
    
  • msilvamsilva Posts: 2Questions: 0Answers: 0

    Very good julicrack !!! I am the several days racking my brain and doing research in the forum to resolve this problem (beginner thing). Someone has to document this improvement in code so that beginners like me do not give up to use this wonderful plug-in. Now I have a difinitiva solution to charge thousands of rows from a database table.

  • CsibeGYILOKCsibeGYILOK Posts: 1Questions: 0Answers: 0
    edited March 2015

    Great solution, however the texts with international characters such as "áéűúőóüö" still didnt appear.

    I added this line to the function sql_connect() in spp.class.php before returning the $db object and now it works.

    $db->exec("set names utf8");

This discussion has been closed.