bitdiego
bitdiego

Reputation: 111

cakephp 3.x and datatable example

i am new with cake php 3.4 and i am trying to build an example with ajax request and datatable here is my controller:

 public function getdata(){

if ($this->request->is('ajax')) {
    $cc = array(
            array('nome'=>'parvez', 'cognome'=>'AA', 'email'=>'101'),
            array('nome'=>'alam', 'cognome'=>'1BB', 'email'=>'102'),
            array('nome'=>'phpflow', 'cognome'=>'CC', 'email'=>'103') );


    $x = array(
            "draw"            => 1,
            "recordsTotal"    => count($cc),
            "recordsFiltered" => count($cc),
            "data"            => $cc
        );
    echo json_encode($x);

} }

then, here is che js code

<script type="text/javascript">
function myfunc(){
   $('#example').DataTable( {
        'processing': true,
        'serverSide': true,
        'ajax':{
            type: 'POST',
            url: "<?php echo Cake\Routing\Router::url(
                array(
                   'controller'=>'posts',
                   'action'=>'getdata',
                    '_ext'=>'json',
                   '_full' => true //for full url path
             ));?>",

            success:function(msg){
                console.log(msg);
            },
            error: function(e) {
                    alert("An error occurred: ");
                    console.log(e);
            }
        }
    } );
}

the action is correctly sent but i always get a message error, in which, in google chrome dev tools, i see a 'null' appended after the data:

{"draw":1,"recordsTotal":3,"recordsFiltered":3,"data":[{"nome":"parvez","cognome":"AA","email":"101"},{"nome":"alam","cognome":"1BB","email":"102"},{"nome":"phpflow","cognome":"CC","email":"103"}]}null

i tried not to json_encode the action response and in chrome i see this stuff after the data array:

App\Controller\PostsController::getdata() - APP/Controller\PostsController.php, line 147 Cake\Controller\Controller::invokeAction() - CORE\src\Controller\Controller.php, line 440 Cake\Http\ActionDispatcher::_invoke() - CORE\src\Http\ActionDispatcher.php, line 119 Cake\Http\ActionDispatcher::dispatch() - CORE\src\Http\ActionDispatcher.php, line 93 Cake\Routing\Dispatcher::dispatch() - CORE\src\Routing\Dispatcher.php, line 60 [main] - ROOT\webroot\index.php, line 37Arraynull

so, the problem arises from the index.php page, but why the hell it returns a null array? thanks

Upvotes: 0

Views: 2043

Answers (1)

Marcos Dantas
Marcos Dantas

Reputation: 866

okay, i think this code is broken, you echo json but, not stop renderer, please see help you.

//On controller
$cc = [
'message' = > 'Hello mr.bitdiego'
];
$this->set('data', $cc);
$this->set('_serialize', ['data']);

On you app controller, when serialize data is found, this altear template and render layout to ajax, see on Template/Layout/Ajax.ctp

But, why are you doing this ? see CakeBook: Create Restful Resources you don't create actions of controller, now you work with resources. :D please see it, Facilitate your life.

Upvotes: 1

Related Questions