0

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

ndm
  • 59,784
  • 9
  • 71
  • 110
bitdiego
  • 113
  • 10
  • The problem does not stem from `index.php`, that's just the start of the stacktrace, apparently an error/exception is happening on the server side. Please check your logs and add the complete error message including its related stacktrace to your question. Also have a look at **https://stackoverflow.com/q/42378793/1392379**, controller actions are not supposed to echo data! – ndm Jun 18 '17 at 15:24

1 Answers1

1

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.

Marcos Dantas
  • 836
  • 1
  • 11
  • 10
  • thanx sebastiao (and ndm). i did not know the restful resources you posted, i will study at it. i was trying to solve a (real) problem for my job: i have to upload xls files with users data (first name, last name, email and so on) and then i would display the items in a data table, pssibly with filtering (and even more editing, deleting items...) so i thought that DataTables could be the best choice. – bitdiego Jun 18 '17 at 23:43
  • Ok, yep. Datables is best choice. Did you solve problem? if solved, please vote the answer. – Marcos Dantas Jun 19 '17 at 16:56
  • argh, sorry, i forgot this! yes, your answer has been very useful, thanx a lot – bitdiego Jun 20 '17 at 03:37