Valid JSON returning as invalid

Valid JSON returning as invalid

jrobtzjrobtz Posts: 1Questions: 1Answers: 0

Hi, I can't link to the page showing the problem because I'm using DataTables.js in the admin console of a Wordpress site.

Here's the DataTables debugger link: http://debug.datatables.net/emiwug

Here's the problem:
I get the DataTables warning: table id=example - Invalid JSON response. For more information about this error, please see http://datatables.net/tn/1 error everytime I try and load the page. However, when viewing the source of the document, I get as follows: https://gist.github.com/broskees/9a8c17911f3a6dcfb2fef1d926c76f65

I know that's a lot of code, but look specifically between lines 211 to 228, and also lines 858 to 1067.

In that, you can see that the JSON is definitely properly validated. I even validated it via https://jsonlint.com/

Help would be greatly appreciated.

Thank you!

Answers

  • kthorngrenkthorngren Posts: 20,448Questions: 26Answers: 4,800

    If you go to the debugger link, click on the Tables tab then click on Server interaction you will see the JSON response Datatables doesn't like. Its a bit strange that it shows your full page code.

    When using the ajax option the expectation is that the data will be fetched from a server. In your case you are simply defining it. Since you aren't fetching it you will want to treat it like Javascript sourced data like this example:
    https://datatables.net/examples/data_sources/js_array.html

    Basically you have:

                "ajax":   << remove
                    {   << remove
        "data": [
            {
                "week": "2017-10-29 through 2017-11-04",
                "total_hours": 29,
    .....
                "employee_user_name": "Joseph Roberts"
            }
        ]
    },   << remove
    

    Where data is inside the ajax option. I think it will work if you remove the 3 lines I noted, essentially removing the option ajax option config and leaving just the data option. This should look like the array of objects example of the data docs.

    Kevin

  • danielj210danielj210 Posts: 9Questions: 2Answers: 0
    edited November 2017

    I'm being told that my JSON is invalid when I'm using exact copies of DataTable.net's own example. Just create a new blank project which is very small and copy and paste the example from the link below, which is DataTable's provided "working" example, and see how it's claiming that the JSON is invalid. How is this possible?

    https://datatables.net/examples/ajax/deep.html

    The reason I'm doing this is because I'm passing valid JSON data to a DataTable in another project and it's loading nothing. The JSON is an exact match of the syntax shown in the example. I figured I'd make sure the example was working and low and behold, it doesn't work. I even did a second project and typed everything by hand just to make sure Windows wasn't copying and pasting incorrectly by some fluke chance but still the same thing.

  • tangerinetangerine Posts: 3,352Questions: 37Answers: 394
    edited November 2017

    @danielj210 , you should really start a new thread rather than hi-jacking @jrobtz's thread.

    Also it is more likely that you will get some assistance if you present your code. It seems unlikely that you have made an exact copy of the example, since the example works.

  • danielj210danielj210 Posts: 9Questions: 2Answers: 0

    He got an answer. Hijacking? I was adding to an issue.

  • allanallan Posts: 62,043Questions: 1Answers: 10,170 Site admin

    @danielj210 - The anti-spam mucking around with your account is frustrating - sorry about this, its getting me in a muddle never mind anything else. I've added a reply to your other thread here now that it has been approved.

    If you are still having problems with it, let's carry on the discussion there rather than splitting it between two threads. If you are having problems, perhaps you can run the debugger on the page and let me know the debug code generated.

    Allan

This discussion has been closed.