从本地文件加载时,数据表抛出“无效的JSON”错误

时间:2022-10-09 18:10:35

I'm trying to pull the following JSON data from a local file into DataTables, but getting an invalid JSON response:

我正在尝试将以下JSON数据从本地文件提取到DataTables,但获取无效的JSON响应:

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

Using:

使用:

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

JSFIDDLE

的jsfiddle

3 个解决方案

#1


2  

It's because you do in fact have invalid JSON. When using datatables, according to the documentation, your data source always needs to be an array: https://www.datatables.net/manual/data

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

Here is what it should look like:

这是它应该是什么样子:

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

When you create JSON, you should always validate it to make sure it is valid - check out http://jsonlint.org

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

#2


2  

Use this command to catch the error instead alert:

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

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

From this link

从这个链接

#3


0  

You had to put the ajax method call(GET or POST)... Anything like this:

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

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

#1


2  

It's because you do in fact have invalid JSON. When using datatables, according to the documentation, your data source always needs to be an array: https://www.datatables.net/manual/data

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

Here is what it should look like:

这是它应该是什么样子:

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

When you create JSON, you should always validate it to make sure it is valid - check out http://jsonlint.org

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

#2


2  

Use this command to catch the error instead alert:

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

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

From this link

从这个链接

#3


0  

You had to put the ajax method call(GET or POST)... Anything like this:

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

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