2013-12-07 26 views
0

我正在使用jQuery.dynatree插件,如何使用AJAX加載JSON格式的數據? 我做的所有的文件中說:jQuery.Dunatree.How如何使用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頁面,不用其他的錯誤。 請求的URL通過AJAX返回有效的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" 
}] 

但結果我有消息「加載錯誤」 AMD僅此而已...... 哪裏是我的錯?

我需要加載一些JSON樹到網頁,樹必須有方法來獲得選定的節點,並通過鼠標進行可視化選擇,這就是我需要的。也許需要使用更簡單的插件?

謝謝!

回答

1

如果使用jQuery ajax加載JSON數據,jQuery將通過JSON.parse方法解析JSON數據。 所以不要把你的JSON數據放在[]中,它會在jQuery ajax方法中調用解析錯誤。

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