jQuery.Dunatree。如何使用AJAX加载JSON树?

时间:2013-12-07 23:35:29

标签: javascript jquery ajax json

我正在使用jQuery.dynatree插件,如何使用AJAX加载JSON格式的数据? 我按照文档中的说法做了所有事情:

<script>
$(function() {
    $( "#Tree" ).dynatree({
        title:"Thetree",
        imagePath:'/jquery/css/',
        selectMode:1,
        initAjax:{
            url:"http://127.0.0.1:2013/jsfolderstree"
            }
    });
});
</script>

JS脚本正确连接到HTML页面,没有错误。 AJAX请求的URL返回有效的JSON:

[{
 "key": "0x55638DB3",
 "children": [
  {
   "key": "0x5D43E57C",
   "children": [
    {
     "key": "0x70A4C1CE",
     "children": [
      {
       "key": "0x799E9590",
       "children": [],
       "expand": true,
       "tooltip": "This is group 5",
       "isFolder": true,
       "title": "Group  5"
      },{
       "key": "0x78C952A8",
       "children": [],
       "expand": true,
       "tooltip": "This is group 6",
       "isFolder": true,
       "title": "Group 6"
      }],
     "expand": true,
     "tooltip": "This is group 3",
     "isFolder": true,
     "title": "Group 3"
    }],
   "expand": true,
   "tooltip": "This is group 1",
   "isFolder": true,
   "title": "Group  1"
  },{
   "key": "0x45B98999",
   "children": [
    {
     "key": "0x6C829354",
     "children": [],
     "expand": true,
     "tooltip": "This is group 4",
     "isFolder": true,
     "title": "Group  4"
    }],
   "expand": true,
   "tooltip": "This is group 2",
   "isFolder": true,
   "title": "Group  2"
  },{
   "key": "0x47BE4570",
   "children": [],
   "expand": true,
   "tooltip": "This is group 7",
   "isFolder": true,
   "title": "Group 7"
  }],
 "expand": true,
 "tooltip": "Main level of tree",
 "isFolder": true,
 "title": "Root"
}]

但结果我有消息“正在加载错误”而已...... 我的错误在哪里?

我需要将一些JSON树加载到网页,树必须有方法来获取所选节点,并且通过鼠标进行视觉选择,这就是我需要的。也许需要使用更简单的插件?

谢谢!

1 个答案:

答案 0 :(得分:1)

如果使用jQuery ajax加载JSON数据,jQuery将通过JSON.parse方法解析JSON数据。 所以不要把你的JSON数据放在[]中,它会在jQuery ajax方法中调用解析错误。

{
 "key": "0x55638DB3",
 "children": [{...}]
 ....
}