2015-10-19 65 views
3

我試圖使用fancytree來顯示從PHP休息服務返回的一些數據。服務返回的數據已通過JSONLint進行驗證,並以fancytree文檔中顯示的格式顯示。fancytree not loading json數據

如果我有開發人員工具窗口(Chrome)打開它顯示文件jquery-1.11.3.min.js:2中的錯誤「未捕獲的錯誤:未實現」。

當我驗證了返回的JSON數據時,我使用高級休息客戶端(Google App),並刪除了包含Advanced Rest Client顯示的字符串的雙引號,然後將值粘貼到JSONLint中。

我的jQuery代碼:

<script type="text/javascript"> 
    $(function(){ 
     var phpAPI = "http://localhost/clubjudge/api/JSONClassTree2"; 
     $.getJSON(phpAPI) 
      .done(function(json) { 
       alert(json); 
       $("#tree").fancytree({ 
       source: json 
       } 
      ); 
      }) 
      .fail(function(jqxhr, textStatus, error) { 
       var err = textStatus + ", "+ error; 
       console.log("Request Failed: "+ err); 
    }); 

}); 
</script> 

任何幫助非常讚賞,因爲我一直在現在這個搞亂了幾天。 PS。我希望包含的代碼格式正確。 '預覽'似乎看起來不正確。

想像我最好發佈JSON數據。

[ 
{"title":"A - Australian Native","key":"1","children":[ 
    {"title":"A1 - Australian Native Dendrobium Species","key":"7"}, 
    {"title":"A2 - Australian Native Any Other Species","key":"8"}, 
    {"title":"A3 - Australian Native Dendrobium Hybrid","key":"9"}, 
    {"title":"A4 - Australian Native Any Other Hybrid","key":"10"}, 
    {"title":"A5 - Australian Native Seedling","key":"11"} 
]}, 
{"title":"B - Cymbidium","key":"2","children":[ 
    {"title":"B1 - Standard Type Cymbidium","key":"3"}, 
    {"title":"B2 - Intermediate Type Cymbidium","key":"4"}, 
    {"title":"B3 - Miniature Type Cymbidium","key":"5"}, 
    {"title":"B4 - Cymbidium Species","key":"6"} 
]} 
] 

對不起,我意識到有很多。再一次,這是Advanced Rest Client顯示的內容(修剪掉外部雙引號後)。

回答

0

您可以加載JSON數據將在請求來源:

$("#tree").fancytree({ 
    source: $.ajax({ 
    url: "/clubjudge/api/JSONClassTree2", 
    dataType: "json" 
    }) 
}); 
0

可能是你的json變量是JSON字符串和FancyTree預期它是JS對象,所以你應該首先使用JSON.parse()。它幫助我的情況。