AJAX JSON format warning

AJAX JSON format warning

scpfiscpfi Posts: 4Questions: 2Answers: 0
edited May 2018 in Free community support

I'm using DataTables with the JS code below:

 $(document).ready(function() {
        var table = $('#bot_table').DataTable( {
            "ajax": "pos.txt",
            "paging":   false,
            "searching": false,
            "columns": [
                { "data": "name_of_instance" },
                { "data": "bot" },
                { "data": "updated" },
                { "data": "position" },
                { "data": "l_s" },
                { "data": "l_t" },
                { "data": "p_l" },
                {
                    "className":      'details-control',
                    "orderable":      false,
                    "data":           null,
                    "defaultContent": ''
                }
            ],
        } );

pos.txt is formatted as below:

{
  "data": [
    {
      "name_of_instance":"vul\os\pos",
      "bot":"VUL",
      "updated":"12.06.2018, 12:33 pm",
      "position":"$1032.12",
      "l_s":"Long",
      "l_t":"L",
      "p_l":"$21.23\(2.12%)"
    },
    {
      "name_of_instance":"vul\os\pos",
      "bot":"VUL",
      "updated":"12.06.2018, 12:33 pm",
      "position":"$1572.12",
      "l_s":"Long/Short",
      "l_t":"L",
      "p_l":"$41.23\(2.02%)"
    },
    {
      "name_of_instance":"vul\os\pos",
      "bot":"VUL",
      "updated":"12.06.2018, 08:33 pm",
      "position":"$1032.12",
      "l_s":"Short",
      "l_t":"T",
      "p_l":"-$5.43\(-0.53%)"
    }
  ]
}

I keep getting a warning:

DataTables warning: table id=bot_table - Invalid JSON response. For more information about this error, please see http://datatables.net/tn/1

and the table does not load. Is there any part of the documentation that can help me with that?
Is there any example I can use and replicate?

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,299Questions: 26Answers: 4,945
    Answer ✓

    The place to start is by following the troubleshooting steps at the link provided in the error:
    http://datatables.net/tn/1

    You will find that it fails the jsonlint validation. The problem is with the backslashes. You will need to either escape them, like this: "name_of_instance": "vul\\os\\pos",, or remove them from the string.

    Kevin

  • scpfiscpfi Posts: 4Questions: 2Answers: 0

    Thanks a lot for that. You saved me!
    I'll make sure to read the resources you've linked.

This discussion has been closed.