【问题标题】:No data returned to jquery datatables from ajax call没有数据从 ajax 调用返回到 jquery 数据表
【发布时间】:2014-04-28 14:42:08
【问题描述】:

这是我第一次尝试datatables。我想调用 ajax 函数从数据库 (MariaDB) 返回数据,并在 php 文件中填充现有 tablerowscells

关于数据字段的代码的简化版本是这样的:

$('#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"},
                 ],
    } );

我尝试过使用和不使用"bServerSide": true,,但无法让数据显示在table 中。 Firebug 返回TypeError: aData is undefined

按照the example 中所说的内容,我在php 中构建了一个函数,该函数返回类似json 的结构。

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

就像我说的,我没有使用datatables 的经验,不知道是使用aData 还是aaData,或者尝试更多。

【问题讨论】:

    标签: php jquery ajax datatables


    【解决方案1】:

    这是 ajax 源的另一个示例:
    http://datatables.net/examples/data_sources/ajax.html

    上面写着:
    DataTables expects an object with an array called "aaData" with the data source.

    这是他们的一些 ajax 返回:

    { "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"]
    ] }
    

    就我而言,我总是使用aaData 来启动表数据。
    除此之外,您可以返回更多选项(如sEchoiTotalRecordsiTotalDisplayRecords、...)以完成渲染。

    【讨论】:

    • 我必须缺少一些东西...我使用了您的示例。在 getLines Php 函数中做了一个 echo "your answer"; 并转义了引号。在萤火虫中,作为对httpGET 方法的响应,有:{ "aaData": [ ["Trident","Internet Explorer 4.0","Win 95+","4"], ["Trident","Internet Explorer 5.0","Win 95+","5"], ["Trident","Internet Explorer 5.5","Win 95+","5.5"], ["Trident","Internet Explorer 6","Win 98+","6"], ["Trident","Internet Explorer 7","Win XP SP2+","7"] ] }。但仍然收到TypeError: aData is undefined 错误。
    • 您是否尝试从示例开始(通过复制源代码),并尝试替换您需要的内容?
    • 这个例子不像我打算做的那样:"sAjaxSource": '../examples_support/json_source.txt' 而不是使用ajax 调用 php 函数。
    【解决方案2】:

    Datatables.net 示例不适用于 MariaDB 服务器(使用不同形式的 root 用户登录时相同 ~ 旧密码加密)。我不知道为什么它没有,但它没有响应任何数据(ajax)。在 Windows MariaDB 服务器上测试。

    解决方案:如果您不想卸载 MariaDB 服务器,请在不同的端口上安装 MySQL 服务器服务/守护程序,然后迁移(导出/导入)数据。

    【讨论】:

    • 真正的解决方案:在 MariaDB/MySQL 连接字符串上进行明确的字符集定义(例如:charset=utf8)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-25
    • 1970-01-01
    • 2011-12-01
    • 1970-01-01
    • 2016-07-25
    • 2016-07-28
    • 1970-01-01
    相关资源
    最近更新 更多