fheider / cakephp-datatables

CakePHP3 Plugin for DataTables plug-in for jQuery
MIT License
14 stars 40 forks source link

AJAX - Invalid JSON Response #5

Closed gillaho closed 9 years ago

gillaho commented 9 years ago

Hi, I need help setting up the datatables. I always get invalid JSON response error message, if I search or order. What do I need to do? Can you give me a hint? Thanks

I have following codes in index action:

        $data = $this->DataTables->find('Users')->all();
        $this->set([
            'data' => $data,
            '_serialize' => array_merge($this->viewVars['_serialize'], ['data'])
        ]);

and in view following codes:

                    <table class="table table-striped table-bordered table-hover dataTable">
                        <thead>
                            <tr>
                                <th>ID</th>
                                <th>Firstname</th>
                                <th>Lastname</th>
                            </tr>
                        </thead>
                        <tfoot>
                        <tr class="table-search info">
                            <td><input type="text" placeholder="Search ..." class="form-control input-sm input-block-level" /></td>
                            <td><input type="text" placeholder="Search ..." class="form-control input-sm input-block-level" /></td>
                            <td><input type="text" placeholder="Search ..." class="form-control input-sm input-block-level" /></td>
                        </tr>
                        </tfoot>
                        <tbody>
                        <?php foreach ($data as $item): ?>
                            <tr>
                                <td><?= $item->id ?></td>
                                <td><?= $item->firstname ?></td>
                                <td><?= $item->lastname ?></td>
                            </tr>
                        <?php endforeach; ?>
                        </tbody>
                    </table>

<?php $this->start('script'); ?>
<script type="text/javascript">
    <?php
        use Cake\Routing\Router;
        $this->DataTables->init([
            'ajax' => [
                'url' => Router::url(['action' => 'dataTableIndex']),
            ],
            'deferLoading' => $recordsTotal,
            'delay' => 600,
            'columns' => [
                [
                    'name' => 'CoreUsers.id',
                    'data' => 'id',
                    'orderable' => false
                ],
                [
                    'name' => 'CoreUsers.firstname',
                    'data' => 'firstname'
                ],
                [
                    'name' => 'CoreUsers.lastname',
                    'data' => 'lastname'
                ]
            ]
        ])->draw('.dataTable');
    ?>
</script>
<?php $this->end(); ?>

Can you please tell me what did I do wrong? Thank you in advance.

fheider commented 9 years ago

Please open the Firebug console to check which error you get. If 500, check your url. If you get another error, please send your complete error message.

gillaho commented 9 years ago

Hi fheider, thanks for the hints. I found out the problems :)