DataTable stuck on processing even if AJAX response is fine. It only shows the loading icon.

DataTable stuck on processing even if AJAX response is fine. It only shows the loading icon.

drev20drev20 Posts: 1Questions: 1Answers: 0
edited October 2025 in Free community support

Link to test case:
Debugger code (debug.datatables.net): ulolit
Error messages shown:


Description of problem:
I'm having a problem. DataTable stuck on loading/processing but it has ajax response. No errors but my DT is not loading any data. I'll provide my scripts here. Thank you.

Javascript:

var period_management_DT = $('#tablePeriod').DataTable({
        'serverSide': true,
        'info' : true,
        'processing' : true,
        ajax : {
            url: "<?php echo base_url('cvs/Period_management/generateAllPeriods'); ?>",
            type: 'POST',
            data: function (d) {
                return {'_pmData' : d}
            },
            success : function (data) {
                console.log(data);  
                
            },
            error: function (err) {
                console.log(err);
                
            }
         },
        'columns' : [
            {data: 'name'},
            {data: 'code'},
            {data: 'months'},
            {data: 'cvs_encoding_status_id'}
        ],
        columnDefs: [
                {
                "defaultContent": "-",
                "targets": "_all"
                }
        ]
    });

Server-side:

public function generateAllPeriods () {
        $postData = $this->input->post('_pmData');
        $draw = intval($postData['draw']);
        $start = intval($postData['start']);
        $length = intval($postData['length']);
        $pm_model = new PeriodModel();
        $data = $pm_model->get_periods_with_months_2($start, $length);

        $resData = array(); 

        foreach ($data[0] as $d) {
            $resData[] = array (
                'name' => $d['name'], 
                'code' => $d['code'],
                'months' => $d['months'],
                'cvs_encoding_status_id' => $d['cvs_encoding_status_id']
            );
         }

        $output = array(
            'draw' => $draw,
            'recordsTotal' => $data[1],
            'recordsFiltered' => $data[1],
            'data' => $resData
        );
       // echo json_encode($output);
       echo json_encode($output);
    }

Edited by Allan - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

Answers

  • kthorngrenkthorngren Posts: 22,381Questions: 26Answers: 5,140

    Possibly you need to remove the success function from the ajax option. The ajax docs state this:

    success - Must not be overridden as it is used internally in DataTables. To manipulate / transform the data returned by the server use ajax.dataSrc (above), or use ajax as a function (below).

    Let us know if removing the success function resolves the issue.

    Kevin

  • DasgoodDasgood Posts: 1Questions: 0Answers: 0

    I have this on my side and it works fine:

    {
            url: URLLink,
            contentType: "application/json",
            type: 'POST',
            dataSrc: function (json) {
                console.log('AJAX Success: Data received from server:', json);
                return json; // Ensure the data is returned for DataTables processing
            },
            error: function (xhr, status, error) {
                console.error('AJAX Error:', error);
                console.error('Status:', status);
                console.error('Response:', xhr.responseText);
            },
            complete: function (xhr, status) {
                console.log('AJAX Complete: Status:', status);
                hideDimOverlay();
            }
        }
    

    try just returning the actual response like:

    data: function (d) {
                    return d;
                },
    

    I also use dataSrc: not data:

  • allanallan Posts: 65,472Questions: 1Answers: 10,874 Site admin

    ajax.dataSrc and ajax.data are two different things. The first tells DataTables where to find the data array in the returned JSON, while the second says what data should be sent to the server-side for each new Ajax request.

    Allan

Sign In or Register to comment.