Json response invalid DataTable Server Side

0

I've been trying for two days to solve a problem I'm having with DataTable related to json's return. I am using the examples available on the DataTable site as well as those available at GitHub .

I'm using this code to create json:

server_processing.php

<?php

$table = 'tab_especialistas_crm';

$primaryKey = 'id_crm';

$columns = array(
    array( 'db' => 'id_crm', 'dt' => 0 ),
    array( 'db' => 'registro_crm', 'dt' => 1 ),
    array( 'db' => 'nome_profissional', 'dt' => 2 ),
    array( 'db' => 'especialidade', 'dt' => 3 ),
    array( 'db' => 'cidade', 'dt' => 4 ),
    array( 'db' => 'estado', 'dt' => 5),
);

$sql_details = array(
    'user' => 'root',
    'pass' => '',
    'db'   => 'db_especialistas',
    'host' => '127.0.0.1'
);

require( 'ssp.class.php' );

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

The ssp.class.php file, required for this code is intact, ie I did not change it, it is exactly as available in git.

The ajax of my html looks like this:

index.php

<script>
$(document).ready(function () {
   $('#tableExames').DataTable({ 
      "processing": true,  
      "serverSide": true,            
      "ajax": "server_processing.php"
   });
});
</script>

I'm getting an alert in the Invalid Json browser. It includes the line $db->exec("set names utf8"); in the function sql_connect , in the ssp.class.php file, thinking that it was something related to the encoding, but it did not resolve.

I made a var_dump:

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

And as unbelievable as it sounds, the mounted array is ok and json is, too, I validated it on JsonLint.

However, I'm still getting the json response invalid alert in the file where the table is.

When debugging the code in the browser, there is no error, however I took a print to help solve the problem:

Problemsolved

Tothosewhohavethesameproblem,Ileaveheretheresolution.Inmycaseitworked!

Ijustremovedthevar_dumpthatIhadplacedtoidentifyanerrorintheserver_processing.phpfilethatworked.

Inshort,justchangethessp.class.phpfile,including$db->exec("set names utf8"); before return. The error was due to the special characters returned in the data coming from mysql.

static function sql_connect ( $sql_details )
    {
        try {
            $db = @new PDO(
                "mysql:host={$sql_details['host']};dbname={$sql_details['db']}",
                $sql_details['user'],
                $sql_details['pass'],
                array( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION )
            );
        }
        catch (PDOException $e) {
            self::fatal(
                "Ocorreu um erro de conexão com o banco de dados. ".
                "O erro retornado pelo servidor foi: ".$e->getMessage()
            );
        }

        $db->exec("set names utf8");
        return $db;
    }
    
asked by anonymous 27.10.2018 / 21:46

0 answers