Basic Problem with Ajax
Basic Problem with Ajax
Hello,
I'm having what would seem to be a very elementary problem using json data from a remote URL.
I have no control over the format of this data and I'd like to either confirm that I'm not going to be able to use it or find a way to do it.
I know I can read the stream in and manipulate it using php on my side to output it again however I want, but this seems the long way round (this is my last resort).
The issue I'm having is that I receive the error:
Uncaught TypeError: Cannot read property 'length' of undefined
The reason for this would appear to be that the json stream returned does not start with [data].
I've read that you can use 'dataSRC' to substitute for this but the json I get returned is in the following format:
{
"itemcount": 15"
},
"items": [
{
"id":1,
"name": "A N Other",
"age": 25,
"gender": "m"
},
.....
I want to render the info in items.
Is there any way I can do this?
Apologies if I haven't explained this very well.
This question has an accepted answers - jump to answer
Answers
It looks like your data is returned in an object called
items
instead ofdata
as described here:https://datatables.net/manual/data/
Not knowing how you have your Datatables config I would guess that you would set the
ajax.dataSrc
to 'items'.Kevin
Hi, thanks for replying Kevin. I think I explained myself poorly previously.
There are other objects than 'items' returned in the json.
{ "itemcount": 15 },
{ "items" : [
{
"id":1,
"name", "A N Other"
}
{
"id":2,
"name","U R Other"
}
]
},
{ "links" : [
{
etc etc etc
}
]
}
Does that make more sense?
I don't seem to be able to get at 'item'.
If I use dataSrc as items I recieve the error message as it cannot map from the root of the json (???)
Is that the actual JSON?
Its not a valid JSON string. You can use https://jsonlint.com/ to validate it.
For example you need a
:
betweenname
and the value. You need commas between the rows (id's 1 and 2).How many columns do you have defined in your table?
What does you Datatables init code look like?
Having other objects returned is ok. Please pot more code snippets. Don't edit what you post so much, its harder to know what you are doing.
Kevin
Hi Kevin,
apologies. I'm not deliberately trying to make things more difficult and thanks for your patience. I just didn't want to post a load of json. I've attached a text file with a sample.
That looks good. Now what about your Datatables config and table columns?
You have nested objects. If you want to display those then this example will show you how:
https://datatables.net/examples/ajax/deep.html
Kevin
Hello again,
I've attached another text file with the javascript and html. I've tried various different configurations (and failed) but this is what I have at the moment.
It just returns no results. I'm guessing I'm doing something very basic wrong.
Try changing this:
to this:
Kevin
as soon as I do that I get:
Uncaught TypeError: Cannot read property 'length' of undefined
This is the initial error I was receiving. If I set dataSrc to 'items' it doesn't like it.
Sorry I missed it earlier. You need to change your column definitions:
Change "items" to "data". You need to define your data with
columns.data
.Kevin
ugh - thanks Kevin.
I was sure I'd tried that at some point and it hadn't worked.
Thanks again.