fancytree 未加载 json 数据
fancytree not loading json data
我正在尝试使用 fancytree 来显示从 php 休息服务返回的一些数据。服务返回的数据已通过 JSONLint 验证,并以 fancytree 文档中显示的格式显示。
如果我有开发者工具 window (Chrome) 打开它会在文件 jquery-1.11.3.min.js:2 中显示错误 "Uncaught Error: Not implemented" .
当我验证返回的 JSON 数据时,我使用的是 Advanced Rest Client(Google 应用程序),我删除了包围 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。我希望包含的代码格式正确。 'preview' 好像不太对。
认为我最好 post 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 显示的内容(在去掉外部双引号之后)。
不确定这是否能解决您的问题,但您不需要发出单独的 ajax 请求。 Fancytree直接支持这个:
$("#tree").fancytree({
source: {
url: "/clubjudge/api/JSONClassTree2",
cache: false
},
...
});
详情请看这里:https://github.com/mar10/fancytree/wiki/TutorialLoadData
您可以加载 JSON 请求的数据来源:
$("#tree").fancytree({
source: $.ajax({
url: "/clubjudge/api/JSONClassTree2",
dataType: "json"
})
});
可能你的json变量是JSON字符串,而FancyTree期望它是JS对象,所以你应该先使用JSON.parse()。它对我的情况有所帮助。
我正在尝试使用 fancytree 来显示从 php 休息服务返回的一些数据。服务返回的数据已通过 JSONLint 验证,并以 fancytree 文档中显示的格式显示。
如果我有开发者工具 window (Chrome) 打开它会在文件 jquery-1.11.3.min.js:2 中显示错误 "Uncaught Error: Not implemented" .
当我验证返回的 JSON 数据时,我使用的是 Advanced Rest Client(Google 应用程序),我删除了包围 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。我希望包含的代码格式正确。 'preview' 好像不太对。
认为我最好 post 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 显示的内容(在去掉外部双引号之后)。
不确定这是否能解决您的问题,但您不需要发出单独的 ajax 请求。 Fancytree直接支持这个:
$("#tree").fancytree({
source: {
url: "/clubjudge/api/JSONClassTree2",
cache: false
},
...
});
详情请看这里:https://github.com/mar10/fancytree/wiki/TutorialLoadData
您可以加载 JSON 请求的数据来源:
$("#tree").fancytree({
source: $.ajax({
url: "/clubjudge/api/JSONClassTree2",
dataType: "json"
})
});
可能你的json变量是JSON字符串,而FancyTree期望它是JS对象,所以你应该先使用JSON.parse()。它对我的情况有所帮助。