JSON from WebApi
JSON from WebApi
Hey Guys!
I have gave it my best to try to solve my problem, but I just cant seem to fix it.
I have a WebAPI returning this json (https://localhost:44330/api/test):
[{"id":1,"perspektiv":"Medarbetare","nyckeltal":"Bla","tidsperiod":"Blä","värde":1,"enhet":"st"},{"id":2,"perspektiv":"Medarbetare","nyckeltal":"BlaBla","tidsperiod":"Blä","värde":1,"enhet":"st"},{"id":3,"perspektiv":"Medarbetare","nyckeltal":"BlaBlaBla","tidsperiod":"Blä","värde":1,"enhet":"st"},{"id":4,"perspektiv":"Medarbetare","nyckeltal":"Bla","tidsperiod":"Blä","värde":1,"enhet":"st"},{"id":5,"perspektiv":"Medarbetare","nyckeltal":"BlaBlaBla","tidsperiod":"Blä","värde":1,"enhet":"st"},{"id":6,"perspektiv":"Medarbetare","nyckeltal":"Bla","tidsperiod":"Blä","värde":1,"enhet":"st"},{"id":6,"perspektiv":"Medarbetare","nyckeltal":"Bla","tidsperiod":"Blä","värde":1,"enhet":"st"}]
I have also tried different ways of consuming this json, and obviously the wrong way.
$('#example').dataTable({
"ajax": { "https://localhost:44330/api/test)"
},
"columns": [
{ "data": "perspektiv" },
{ "data": "nyckeltal" },
{ "data": "tidsperiod" },
{ "data": "värde" },
{ "data": "enhet" },
]
});
});
At best I get: "Datatables: Uncaught TypeError: Cannot read property 'length' of undefined"
Can anyone help me?
/Simon
Replies
You need to use
ajax.dataSrc
and set it to an empty string to tell DataTables to expect just an array of data.Allan
Thank you Allan, you're freakin' best :D