Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError with data via url #22

Open
Wonko52 opened this issue Mar 9, 2022 · 2 comments
Open

TypeError with data via url #22

Wonko52 opened this issue Mar 9, 2022 · 2 comments

Comments

@Wonko52
Copy link

Wonko52 commented Mar 9, 2022

I got a JS-Error when I try to get data via Url:

Uncaught TypeError: can't convert undefined to object

This is the code:

    const myTree = new Tree('#myTree', {
      url: 'http://localhost:8000/treeviewJson',
      method: 'GET',
    });

But this works:

let url = 'http://localhost:8000/treeviewJson';

fetch(url)
.then(res => res.json())
.then(out => {
  
  const myTree = new Tree('#myTree', {
    data: [out],
    })
}
  );

This is the response of the URL

# curl http://localhost:8000/treeviewJson
{"id":"0","text":"Node 0","children":[{"id":1,"text":"Node 0-1"},{"id":2,"text":"Node 0-2"}]}[]

It's generate by PHP:

function treeviewJson(): Response
    {
        $data = [
            'id' => '0',
            'text' => 'Node 0',
            'children' => [
                [
                    'id' => 1,
                    'text' => 'Node 0-1'
                ],
                [
                    'id' => 2,
                    'text' => 'Node 0-2'
                ],
            ]
        ];



        $response = new Response();
        $response->setContent(json_encode($data));
        $response->headers->set('Content-Type', 'application/json');
        return $response;
}
@DarthSonic
Copy link

I get the same exception: #25

@THenkeDE
Copy link

maybe just my 2cent but your return should be an array itself like

        $data = [
[
            'id' => '0',
            'text' => 'Node 0',
            'children' => [
                [
                    'id' => 1,
                    'text' => 'Node 0-1'
                ],
                [
                    'id' => 2,
                    'text' => 'Node 0-2'
                ],
            ]
]
        ];

that will result in this json

[
  {
    "id": "0",
    "text": "Node 0",
    "children": [
      {
        "id": 1,
        "text": "Node 0-1"
      },
      {
        "id": 2,
        "text": "Node 0-2"
      }
    ]
  }
]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants