Invalid JSON response on last page of pagination

Invalid JSON response on last page of pagination

Murat ÖzalkayaMurat Özalkaya Posts: 3Questions: 1Answers: 0

Debugger results: #marka
Data source: Ajax
Processing mode: Server-side
Draws: 4
Columns: 2
Rows - total: 10
Rows - after search: 10
Display start: 30

Display length: 10

Error message= http://datatables.net/tn/1

Description of problem: I was testing server-side processing on datatable with 40 rows.I ddn't change the default entry limit. It was working very well until the last page. When i click on 4th page "Invalid JSON response " error occures. I've been searching on internet for few days, but i couldn't find(or understand the solutions because i'm new at it). Could someone please explain me,how to solve this? Thanks in advance.

Here is my codes.

index.html :

<html>
<head>
    <!-- Latest compiled and minified CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">

    <!-- jQuery library -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

    <!-- Popper JS -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>

    <!-- Latest compiled JavaScript -->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>

    <link rel="stylesheet" type="text/css" href="DataTables/datatables.min.css"/>  
    <script src="DataTables/datatables.min.js"></script> 
</head>
<body>

<table id="marka" class="display" style="width:100%">
        <thead>
            <tr>
                <th>id</th>
                <th>ismi</th> 
            </tr>
        </thead>
        <tfoot>
            <tr>
                <th>id</th>
                <th>ismi</th> 
            </tr>
        </tfoot>
    </table>
</body>
<script>
    $(document).ready(function() {
    $('#marka').DataTable( {
        "processing": true,
        "serverSide": true,
        "ajax": {
            "url": "serverside.php",
            "type": "POST"
        },
        "columns": [
            { "data": "id" },
            { "data": "ismi" }
        ]
    } );
} ); 
    </script>
</html>

serverside.php:
/*
* DataTables example server-side processing script.
*
* Please note that this script is intentionally extremely simple 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 = 'marka';

        // 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 object
        // parameter names
        $columns = array(
            array( 'db' => 'id', 'dt' => 'id' ),
            array( 'db' => 'ismi',  'dt' => 'ismi' ) 
        ); 
        // SQL server connection information
        $sql_details = array(
            'user' => 'root',
            'pass' => '',
            'db'   => 'pandoraticaret_takip',
            '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( $_POST, $sql_details, $table, $primaryKey, $columns )
        ); 

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 15,112Questions: 1Answers: 2,583

    What's the response from the server when you get that error?

    Colin

  • Murat ÖzalkayaMurat Özalkaya Posts: 3Questions: 1Answers: 0

    It returns blank page. Thank you for paying attention. I think, i found the reason why this occurs. As you can see in 37th row of table, there is a speacial char "ü". json_encode doesn't convert it, so that pagination breaks at 37th entry. I found the reason but still don't have solution :smile:

    CREATE TABLE IF NOT EXISTS marka (
    id int(11) NOT NULL AUTO_INCREMENT,
    ismi varchar(255) COLLATE utf8mb4_turkish_ci NOT NULL,
    PRIMARY KEY (id)
    ) ENGINE=MyISAM AUTO_INCREMENT=50 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_turkish_ci;

    --

    -- Tablo döküm verisi marka

    INSERT INTO marka (id, ismi) VALUES
    (1, '@phone'),
    (2, 'Alcatech'),
    (3, 'Alcatel'),
    (4, 'Anka'),
    (5, 'Apple'),
    (6, 'Asus'),
    (7, 'BB Mobile'),
    (8, 'BlackBerry'),
    (9, 'Blackview'),
    (10, 'C5 Mobile'),
    (11, 'Casper'),
    (12, 'Concord'),
    (13, 'Day Mobile'),
    (14, 'EvoBT'),
    (15, 'General Mobile'),
    (16, 'Gigaset'),
    (17, 'Hiking'),
    (18, 'Honor'),
    (19, 'HTC'),
    (20, 'Huawei'),
    (21, 'Lenovo'),
    (22, 'LG'),
    (23, 'Meizu'),
    (24, 'Motorola'),
    (25, 'Nokia'),
    (26, 'Oppo'),
    (27, 'Oppo Realme'),
    (28, 'Palm'),
    (29, 'Premier'),
    (30, 'Realme'),
    (31, 'Reeder'),
    (32, 'Samsung'),
    (33, 'Sony'),
    (34, 'TCL'),
    (35, 'Tinmo'),
    (36, 'TP - Link'),
    (37, 'Türk Telekom'),
    (38, 'Vestel'),
    (39, 'Xiaomi'),
    (40, 'ZTE');
    COMMIT;

  • Murat ÖzalkayaMurat Özalkaya Posts: 3Questions: 1Answers: 0

    I finally fix it :) I wrote"$db->query("SET NAMES UTF8");" at 400th row of ssp.class.php, now it runs without an error.

  • colincolin Posts: 15,112Questions: 1Answers: 2,583
    Answer ✓

    Excellent, good spot, glad all sorted,

    Colin

This discussion has been closed.