cant pass AJAX loaded JSON as variable to core data

727 views
Skip to first unread message

seanr...@hotmail.co.uk

unread,
Aug 6, 2014, 5:00:14 AM8/6/14
to jst...@googlegroups.com
Hi, I am trying to load JSON from the server through an AJAX call but i keep getting this error 'Uncaught Error: Syntax error, unrecognized expression: "my json from the server" the JSON being returned is being converted to the pattern that jstree states, i.e, 

[
                   { "id": "ajson2", "parent": "#", "text": "Root node 2" },
                   { "id": "ajson3", "parent": "ajson2", "text": "Child 1" },
                   { "id": "ajson1", "parent": "#", "text": "Simple root node" },


I am using jstree version 3.0.2, Jquery 1.11.1 and angularjs 1.0.6.

Below is the code i am using to do the conversion and load the tree. any help would be much appreciated

 $(document).ready(function () {

                                            var treedata = [];

                                            $.ajax({
                                                url: "my JSON url",
                                                type: "GET",
                                                dataType: "JSON",
                                                headers:
                                                {
                                                    "I attach an API key here",
                                                    "accept": "application/json; charset=utf-8",
                                                    "Content-Type": "application/json"
                                                },
                                                success: function (data, txtStatus, request) {
                                                    return CallAjaxSuccess(data, txtStatus, request);
                                                },
                                                error: function (request, txtStatus, err) {
                                                    var errorData = [];
                                                    return errorData;
                                                }
                                            });

                                            function CallAjaxSuccess(data, txtStatus, request) {

                                                if (request.status === 200) {
                                                    if (data) {

                                                        data.folders.forEach(function (folder) {

                                                            var string;

                                                            if (folder.parent == '0@') {    //Root folders in my JSON from the server has id: 0@
                                                                string = '#';
                                                            } else {
                                                                string = folder.parent;
                                                            }                                                              

                                                            var nodes = {
                                                                id: folder.id,
                                                                text: folder.name,
                                                                parent: string
                                                            }
                                                            treedata.push(nodes);
                                                            return treedata;
                                                        });
                                                    }
                                                }

                                                $('#jstree1').jstree({
                                                    'plugins': ["wholerow"],
                                                    'core': {
                                                        'data': JSON.stringify(treedata)            //If i leave out the JSON.stringify i get error: Uncaught TypeError: Cannot read property 'children' of undefined


                                                    }

                                                });
                                            }

                                        });

Ivan Bozhanov

unread,
Aug 6, 2014, 5:26:41 AM8/6/14
to jst...@googlegroups.com
Use: data : treedata (no JSON.stringify).
When feeding data directly to jstree it needs to be JSON, not a string.

Best regards,
Ivan

seanr...@hotmail.co.uk

unread,
Aug 6, 2014, 6:00:39 AM8/6/14
to jst...@googlegroups.com
Hi Ivan, I have tried that but i get the following error: Uncaught TypeError: Cannot read property 'children' of undefined

Ivan Bozhanov

unread,
Aug 6, 2014, 7:23:56 AM8/6/14
to jst...@googlegroups.com
Just before creating the tree - console.log(treedata) and tell me what it contains - it should be the array you mentioned above, and not a string.

Best regards,
Ivan

seanr...@hotmail.co.uk

unread,
Aug 6, 2014, 7:34:15 AM8/6/14
to jst...@googlegroups.com

Hi Ivan,

It is an array of objects so what is see in the console is the following [Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object]. 

cWhen expanded it shows the details:
  1. 0Object
    1. id"4267744@D1_BK"
    2. parent"#"
    3. text"Defect 865"
    4. __proto__Object
  2. 1Object

Ivan Bozhanov

unread,
Aug 6, 2014, 9:41:45 AM8/6/14
to jst...@googlegroups.com
Then there must be something wrong with your data - it is a very common problem here in the forum - when using the alternative JSON format (the one with ID and PARENT) make sure the structure is valid. The error you are describing is typical if you have a node, which has a value for parent, that is not present in the structure as ID (aside from "#"). For example JSON like this will trigger the error:
[
 { id : "1", parent : "#", text : "root" },
 { id : "2", parent : "1", text : "child" },
 { id : "3", parent : "4", text : "child with invalid parent" }
]

Check your structure.

Best regards,
ivan

seanr...@hotmail.co.uk

unread,
Aug 6, 2014, 11:22:16 AM8/6/14
to jst...@googlegroups.com
Hi Ivan,

That was the case. This has hi-lighted some issues from my end where you can delete a folder but leave the sub folders linked to a non-existent parent. There are also issues where a root folder and all its contents can be in the subfolder of a separate folder.

Cheers
Sean
Reply all
Reply to author
Forward
0 new messages