treejs
treejs copied to clipboard
TypeError with data via url
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;
}
I get the same exception: #25
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"
}
]
}
]