Basic Problem with Ajax

Basic Problem with Ajax

dtabnewbdtabnewb Posts: 9Questions: 2Answers: 0

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

  • kthorngrenkthorngren Posts: 20,317Questions: 26Answers: 4,772

    It looks like your data is returned in an object called items instead of data 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

  • dtabnewbdtabnewb Posts: 9Questions: 2Answers: 0

    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 (???)

  • kthorngrenkthorngren Posts: 20,317Questions: 26Answers: 4,772

    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 : between name 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

  • dtabnewbdtabnewb Posts: 9Questions: 2Answers: 0

    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.

  • kthorngrenkthorngren Posts: 20,317Questions: 26Answers: 4,772

    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

  • dtabnewbdtabnewb Posts: 9Questions: 2Answers: 0

    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.

  • kthorngrenkthorngren Posts: 20,317Questions: 26Answers: 4,772

    Try changing this:

                "ajax": {
                    "url": "/indices.php",
                    "dataSrc": ""
                },
    

    to this:

                "ajax": {
                    "url": "/indices.php",
                    "dataSrc": "items"
                },
    

    Kevin

  • dtabnewbdtabnewb Posts: 9Questions: 2Answers: 0

    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.

  • kthorngrenkthorngren Posts: 20,317Questions: 26Answers: 4,772
    Answer ✓

    Sorry I missed it earlier. You need to change your column definitions:

                "columns": [
                    {"items": "name"},
                    {"items": "price"},
                    {"items": "oneDayReturn" },
                    {"items": "oneMonthReturn" },
                    {"items": "sixMonthReturn" },
                    {"items": "oneYrReturn" },
                    {"items": "threeYrReturn" },
                    {"items": "fiveYrReturn" },
                    {"items": "tenYrReturn" },
                    {"items": "fifteenYrReturn" },
                    {"items": "divYield" }
                ]
    
    

    Change "items" to "data". You need to define your data with columns.data.

    Kevin

  • dtabnewbdtabnewb Posts: 9Questions: 2Answers: 0

    ugh - thanks Kevin.

    I was sure I'd tried that at some point and it hadn't worked.
    Thanks again.

This discussion has been closed.