Invalid JSon error

Invalid JSon error

jalapejalape Posts: 117Questions: 2Answers: 1

Good afternoon everyone,
I have a problem filling a table from invalid json. In the forum I have found a link that describes the steps to detect the error: https://datatables.net/manual/tech-notes/1
The JSON Lint tool confirms that the Json format is not valid, the problem is that I don't know how to make it so.
I leave the link to the table: http://agenda.javierlasen.es/favorito
Thanks

Replies

  • kthorngrenkthorngren Posts: 21,322Questions: 26Answers: 4,948

    The problem is at the end of the JSON response:

        "files": [],
        "searchPanes": {
            "options": []
        }
    }
    NULL
    

    Looks like your server script is adding NULL to the response. Start by looking at what is returned from your your server script.

    Kevin

  • jalapejalape Posts: 117Questions: 2Answers: 1

    Thanks for answering Kevin:
    This is the structure I am using:
    The view has the table and calls the controller via ajax

    <table class="table table-bordered table-hover" id="favorito_tbl" width="100%" cellspacing="0">
         <thead>
           <tr>
             <th>Materia</th>
             <th>Título</th>
             <th>Web</th>
             <th>Pal.<span class="espacio">_</span>Clave</th>
             <th>Detalles</th>
         </thead>
    </table>
    
    
    <script>
        window.onload = function(){
            var editor;
    
            $(document).ready(function() {
                $('#favorito_tbl').DataTable( {
                    responsive: true,
                    dom: "Blfrtip",
                    ajax: "<?= route_to("favorito_datatable") ?>",
                    select:     true,
                } );
            } );
        }
    </script>
    

    In the controller I have the function to collect the model data

    public function datatable(){
       $datatable = new DatatableModel();  
    }
    

    The model

    <?php namespace App\Models;
    
    require_once(APPPATH.'Libraries/php_editor/DataTables.php');
    
    // Alias Editor classes so they are easy to use
    use
        DataTables\Editor,
        DataTables\Editor\Field,
        DataTables\Editor\Format,
        DataTables\Editor\Mjoin,
        DataTables\Editor\Options,
        DataTables\Editor\Upload,
        DataTables\Editor\Validate,
        DataTables\Editor\ValidateOptions;
    
    use CodeIgniter\Model;
    use DataTables\Database;
    
    class DatatableModel extends Model{
    
        protected $table = 'tb_favorito';
        protected $primaryKey = 'id';
        
       
        public function getDatatable(){
    
            $sql_details = array(
                "type" => "Mysql",     // Database type: "Mysql", "Postgres", "Sqlserver", "Sqlite" or "Oracle"
                "user" => "root",          // Database user name
                "pass" => "",          // Database password
                "host" => "localhost", // Database host
                "port" => "",          // Database connection port (can be left empty for default)
                "db"   => "qadu020",          // Database name
                "dsn"  => "charset=utf8mb4",          // PHP DSN extra information. Set as `charset=utf8mb4` if you are using MySQL
                "pdoAttr" => array()   // PHP PDO attributes array. See the PHP documentation for all options
            );
    
            $db = new Database( $sql_details );
     
            /*
            * Example PHP implementation used for the join.html example
            */
            Editor::inst( $db, 'tb_favorito' )
            ->field( 
                Field::inst( 'tb_favorito.materia_id' )
                ->validator( Validate::notEmpty( ValidateOptions::inst()
                    ->message( 'El campo materia es necesario' )    
                ) ) 
                ->options( Options::inst()
                    ->table( 'vista_materia' )
                    ->value( 'id' )
                    ->label( 'palabra' )
                ),
                Field::inst( 'vista_materia.palabra' ),
    
                Field::inst( 'tb_favorito.titulo' ),
                Field::inst( 'tb_favorito.ruta' ),
                Field::inst( 'tb_favorito.detalle' )        
            )
            ->leftJoin( 'vista_materia', 'vista_materia.id', '=', 'tb_favorito.materia_id' )
    
            ->join(
                Mjoin::inst( 'tb_palabra' )
                ->link( 'tb_favorito.id', 'palabra_favorito.media_id' )
                ->link( 'tb_palabra.id', 'palabra_favorito.palabra_id' )
                ->order( 'palabra asc' )
                ->fields(
                    Field::inst( 'id' )
                    ->validator( Validate::required() )
                    ->options( Options::inst()
                        ->table( 'tb_palabra' )
                        ->value( 'id' )
                        ->label( 'palabra' )
                    ),
                    Field::inst( 'palabra' )
                )
                ->where( 'palabra_favorito.modo_id', 4 )
    )
    
    ->process($_POST)
    ->json();
    
  • allanallan Posts: 63,489Questions: 1Answers: 10,470 Site admin

    Nothing obviously would be sending out a NULL there, but also the class is incomplete and it might well be in some of the supporting code. Perhaps search for echo and print statements.

    Allan

  • jalapejalape Posts: 117Questions: 2Answers: 1

    I have modified the Model and now it works. Thank you very much for your time.

    <?php namespace App\Models;
    
    require_once(APPPATH.'Libraries/php_editor/DataTables.php');
    
    use
        DataTables\Editor,
        DataTables\Editor\Field,
        DataTables\Editor\Format,
        DataTables\Editor\Mjoin,
        DataTables\Editor\Options,
        DataTables\Editor\Upload,
        DataTables\Editor\Validate,
        DataTables\Editor\ValidateOptions;
    
    use CodeIgniter\Model;
    
    
    Editor::inst( $db, 'tb_favorito' )
    ->field( 
    
    
        Field::inst( 'tb_favorito.materia_id' )
        ->validator( Validate::notEmpty( ValidateOptions::inst()
            ->message( 'El campo materia es necesario' )    
        ) ) 
        ->options( Options::inst()
            ->table( 'vista_materia' )
            ->value( 'id' )
            ->label( 'palabra' )
        ),
        Field::inst( 'vista_materia.palabra' ),
    
        Field::inst( 'tb_favorito.titulo' ),
        Field::inst( 'tb_favorito.ruta' ),
        Field::inst( 'tb_favorito.detalle' )        
    )
    
    ->leftJoin( 'vista_materia', 'vista_materia.id', '=', 'tb_favorito.materia_id' )
    
    ->join(
        Mjoin::inst( 'tb_palabra' )
        ->link( 'tb_favorito.id', 'palabra_favorito.media_id' )
        ->link( 'tb_palabra.id', 'palabra_favorito.palabra_id' )
        ->order( 'palabra asc' )
        ->fields(
            Field::inst( 'id' )
            ->validator( Validate::required() )
            ->options( Options::inst()
                ->table( 'tb_palabra' )
                ->value( 'id' )
                ->label( 'palabra' )
            ),
            Field::inst( 'palabra' )
        )
        ->where( 'palabra_favorito.modo_id', 4 )
    )
    
    ->process($_POST)
    ->json();
    
    
    class DatatableModel extends Model{
     
    }
    
This discussion has been closed.