Loading an array of objects via Ajax gives error

Loading an array of objects via Ajax gives error

cocovancocovan Posts: 2Questions: 1Answers: 0

I have simple API endpoint that returns data in the following format:

[{id: 1, name: "this", code:" that"}, {id: 2, name: "what", code: "where"}]

If I made this data static within the page, DataTables displays it fine, using the columns property to state where the data comes from.

When I load it via AJAX, an error is thrown. The shape of my response data seems to match the simple array of data example in the manual - https://www.datatables.net/manual/ajax

Example here - comment out the ajax and dataSrc props and uncomment the data prop to switch between the two loading methods.

http://live.datatables.net/qasabufo/2/edit?html,js,console,output

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,173Questions: 26Answers: 4,923
    Answer ✓

    The ajax.dataSrc is part of a configuration object for the ajax option. In your example you have this:

        ajax: "https://5fabc23003a60500167e7153.mockapi.io/scheduler/api/projects",
        //dataSrc: "",
    

    You need to combine the url and dataSrc into one object like this:

        ajax: {
          url: "https://5fabc23003a60500167e7153.mockapi.io/scheduler/api/projects",
          dataSrc: ""
        },
    

    Kevin

  • cocovancocovan Posts: 2Questions: 1Answers: 0

    @kthorngren -- that's the one! Thanks very much. Stared at it for so long I didn't even spot it.

This discussion has been closed.