Datatables throwing "invalid JSON" error when loaded from local file

I am trying to pull the following JSON data from a local file into DataTables, but I get a response invalid 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

+3


source to share


3 answers


This is because you actually have invalid JSON. When using data as per the documentation, your data source should always be an array: https://www.datatables.net/manual/data

This is how it should look:



{
    "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. - http://jsonlint.org

+2


source


Use this command to catch an error instead of a warning:

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

      



From this link

+2


source


You needed to put an ajax method call (GET or POST) ... Something like this:

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

      

0


source







All Articles