我正在使用dhtmlxtree,并且在通过外部脚本从json加载数据时遇到问题。我收到以下错误。有任何想法吗?谢谢!
ErrorType: LoadXML
Error: Incorrect JSON
我的HTML看起来像这样:
<body>
<div id="treeBox" style="width: 200px; height: 200px; overflow: auto;"></div>
<script>
jQuery( document ).ready(function() {
tree = new dhtmlXTreeObject("treeBox", "100%", "100%", 0);
tree.setSkin('dhx_skyblue');
tree.setImagePath("../sma-js/dhtmlxtree/img/csh_bluebooks/");
tree.setXMLAutoLoading("sma-php/loadcustomers.php");
tree.setDataMode("json");
//load first level of tree;
tree.loadJSON("sma-php/loadcustomers.php?id=PNR0000000001");
});
当我手动运行脚本时,返回的JSON如下所示:
[{"phys_addr_state":"ACT","phys_addr_postcode":"2167","install_address":"2 Eade Street - Radiative, John"},{"phys_addr_state":"NSW","phys_addr_postcode":"2263","install_address":"69 The Corso - Flare, Steve"}, {"phys_addr_state":"NSW","phys_addr_postcode":"2112","install_address":"17 Price Street - Solar, Anita"},{"phys_addr_state":"QLD","phys_addr_postcode":"4001","install_address":"71 Eagle Street - Corona, Linda"},{"phys_addr_state":"VIC","phys_addr_postcode":"3053","install_address":"15 Lygon Street - Photon, Marco"}]
我注意到的问题是我的JSON的结构/层次结构。它包含每个记录的项目,我在dhtml站点中看到的其他示例显示“分层”JSON记录。我在服务器上的编码是通过以下方式完成的:
echo json_encode($data);
JSON的另一个dhtml示例(用于比较)是:
{ id: 0, item: [{ id: 1, text: "1111" }, { id: 2, text: "222222", item: [{ id: "21", text: "child" }] }, { id: 3, text: "3333" }]}
是否有不同的方法对sql记录进行编码,根据上述每个例子,每个“id:2项目”的相同条目下,所有相同状态的记录将被组合在一起?
我的标题包括以下内容:
<script src="../sma-js/d3.v3/d3.v3.min.js" charset="utf-8"></script>
<link rel="stylesheet" type="text/css" href="../sma-js/dhtmlxtree/css/dhtmlxtree.css">
<script src="../sma-js/dhtmlxtree/js/dhtmlxcommon.js"></script>
<script src="../sma-js/dhtmlxtree/js/dhtmlxtree.js"></script>
<script src="../sma-js/dhtmlxtree/js/dhtmlxtree_json.js"></script>
答案 0 :(得分:0)
您应该使用特定的JSON格式,与dhtmlx演示中的相同。但是,使用dhtmlxConnector(免费使用dhtmlx组件)而不是自定义脚本会更容易,更快捷。 Connector有助于生成XML输出,您只需要为树XML设置表字段。