数据表投掷"无效的JSON"从本地文件加载时出错

时间:2015-06-10 21:10:38

标签: jquery ajax json datatables

我试图将以下JSON数据从本地文件提取到DataTables中,但获得invalid JSON响应:

    {
    "data": [
        {
            "accountNumber": "2423",
            "domain": "domain.com",
            "playerClass": "",
            "adTag": ""
        },
        {
            "level": "info",
            "message": "generator ",
            "timestamp": "2015-06-10T15:59:02.803Z"
        }
    ]
}

使用:

    $(document).ready(function () {
        $('#content').dataTable({
            "ajax": 'test.log'
        });
    });

JSFIDDLE

3 个答案:

答案 0 :(得分:2)

这是因为你确实拥有无效的JSON。使用数据表时,根据文档,您的数据源始终需要是一个数组:https://www.datatables.net/manual/data

以下是它的样子:

{
    "data": [
        {
            "accountNumber": "1234",
            "domain": "domain.com",
            "playerClass": "Player",
            "adTag": ""
        },
        {
            "accountNumber": "1234",
            "domain": "domain.com",
            "playerClass": "Player",
            "adTag": ""
        }
    ],
    "level": "info",
    "message": "tag generator ",
    "timestamp": "2015-06-09T21:00:45.776Z"
}

创建JSON时,应始终对其进行验证以确保其有效 - 请查看http://jsonlint.org

答案 1 :(得分:2)

使用此命令来捕获错误而不是警告:

$.fn.dataTable.ext.errMode = 'throw';

从此link

答案 2 :(得分:0)

你必须把ajax方法调用(GET或POST)... 这样的事情:

"processing": true,
"serverSide": true,
"ajax": {
    "url": "test.log",
    "type": "POST"
}