Add data from Json (ajax)

Add data from Json (ajax)

sebvegsebveg Posts: 2Questions: 1Answers: 0

Hello, I want to load data from the server and show them in my tab but I always get this mistake :
'DataTables warning: table id=tab_projet - Invalid JSON response. For more information about this error, please see http://datatables.net/tn/1'

Here my JS code :

$.getJSON("https://{AJAX_URL}", function(json)
    {
        var thedata = json;
        $('#tab_projet').DataTable( {
            "processing": true,
            "ajax": thedata,
            "columns": [
                { "data": "nom" },
                { "data": "date_demande" },
                { "data": "status" },
                { "data": "type" },
                { "data": "id_responsable" }
            ]
        });
    });

I checked again and again the json sent by the server and it looks correct. Here a copy/paste from what I get in the chrome developper tools from the ajax request :

{"data":[{"id":"18","type":"creation","date_demande":"2020-05-08 11:19:34","id_user":"2","nom":"test","status":"open","id_responsable":null}],"csrf_hash":"0aa139sdffs117745346ef1"}

Important point : I want the data to be loaded with Jquery and not directly with Datatable ( I dont want to use "ajax": {
"url": "scripts/post.php",
"type": "POST"
} )

I also add my php code (Codeigniter) :

$ajax['data'] = $projets;
$ajax['csrf_hash'] = $this->security->get_csrf_hash();

        $this->output
        ->set_content_type('application/json')
        ->set_output(json_encode($ajax));

This question has accepted answers - jump to:

Answers

  • colincolin Posts: 15,240Questions: 1Answers: 2,599
    Answer ✓
    "ajax": thedata,
    

    isn't an ajax call, which is what's expected.

    Remove that line, and instead add:

    data: thedata.data,
    

    Colin

  • kthorngrenkthorngren Posts: 21,310Questions: 26Answers: 4,948
    Answer ✓

    The problem is you configured the ajax option: "ajax": thedata,. Instead use the data option. Based on the above use "data": thedata.data,.

    Kevin

  • sebvegsebveg Posts: 2Questions: 1Answers: 0

    it works ! thank you !

This discussion has been closed.