简体   繁体   中英

No data returned to jquery datatables from ajax call

This is my first try with datatables . I'd like to call an ajax function to return data from the database ( MariaDB ) and populate the rows and cells of an existing table in the php file.

The simplified version of the code regarding datafields is this:

$('#tbl_resultados').dataTable( {
        "bProcessing": true,
        "sAjaxSource": "/apls/indicadores/indic_acess.php",
        "sAjaxDataProp": "data",
        "fnServerData": function ( sSource, aoData, fnCallback ) {
        request = $.ajax({
              "dataType": 'json', 
              "type": "GET", 
              "url": sSource, 
              "data":{ 
                  "fn": "lines",
                  "indic_per": indic_per,
                        },
              "success": fnCallback
            });
      },

      "aoColumns": [
            { "mDataProp": "id"},
            { "mDataProp": "per"},
                 ],
    } );

I've tried with and without "bServerSide": true, but can't get data to be displayed in the table . Firebug returns TypeError: aData is undefined .

Following what is being said in the example I've built a function in php that returns a json like structure.

function getLines()
{                                                                                               
echo "{ \"aData\":[
    [
         \"25983\",
         \"2010\"
    ],                                         
    [
        \"90420\",
        \"2011\"
    ]
    ]
    }";
}

Like I said, I've no experience with datatables don't know whether to use aData or aaData , or what to try more.

Here is an other example for ajax source :
http://datatables.net/examples/data_sources/ajax.html

It says :
DataTables expects an object with an array called "aaData" with the data source.

Here is some of their ajax return :

{ "aaData": [
    ["Trident","Internet Explorer 4.0","Win 95+","4","X"],
    ["Trident","Internet Explorer 5.0","Win 95+","5","C"],
    ["Trident","Internet Explorer 5.5","Win 95+","5.5","A"],
    ["Trident","Internet Explorer 6","Win 98+","6","A"],
    ["Trident","Internet Explorer 7","Win XP SP2+","7","A"],
    .
    .
    .
    ["Misc","Links","Text only","-","X"],
    ["Misc","Lynx","Text only","-","X"],
    ["Misc","IE Mobile","Windows Mobile 6","-","C"],
    ["Misc","PSP browser","PSP","-","C"],
    ["Other browsers","All others","-","-","U"]
] }

On my side, I always use aaData to initiate table data.
Beside this, you could return more options (like sEcho , iTotalRecords , iTotalDisplayRecords , ...) in order to complete your rendering.

Datatables.net examples don't work with MariaDB server (same for login using users different form root ~ old password encryption). I don't know why it doesn't, but it doesn't respond any data (ajax). Tested on Windows MariaDB server.

Solution: install a MySQL server service/daemon on different port if you don't want to uninstall MariaDB server, and you migrate (export/import) data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM