qiweiren
qiweiren

Reputation: 103

jstree AJAX doesn't work, has a Syntax error: unrecognized expression for the json

I use the jstree API to request the json data with AJAX, but it doesn't work, and it shows me the error:

Error: Syntax error, unrecognized expression: {"results":[{"id":"a","parent":"b","text":"hello","icon":"something..."}, {"id":"b","parent":"#","text":"world","icon":"somethingelse..."}]}

It seems the json is the right format, but the AJAX cannot recognize it.

My AJAX code like this:

$('#jstree').jstree({ 
  'core' : {
      'data' : {
          'url' : 'loadjsTree.php',
          'data' : function (node) {
              return { 'id' : node.id };
          }
      }, 
  }
});

And I do like the API demo:link to the demo

Why it doesn't work? Cound someone help me? Thank you!

Upvotes: 2

Views: 2459

Answers (1)

John
John

Reputation: 66

Have you tried removing the "results" from your object?

{"results":[{"id":"a","parent":"b","text":"hello","icon":"something..."}, {"id":"b","parent":"#","text":"world","icon":"somethingelse..."}]}

to

[{"id":"a","parent":"b","text":"hello","icon":"something..."}, {"id":"b","parent":"#","text":"world","icon":"somethingelse..."}]

The JSON structure for jstree - http://www.jstree.com/docs/json/

You could also try adding 'dataType': 'JSON'...

'url' : 'loadjsTree.php',
'dataType': 'JSON',
'data' : function (node) {
return { 'id' : node.id };
}

I read that it was no longer required but it helped me.

Upvotes: 5

Related Questions