问题描述
我正在根据使用以下代码找到的示例加载数据。它允许我指定一个 url 并使用示例的 url https://www.jstree.com/fiddle/?lazy 我在我的页面上得到一个工作树。这个 url 返回一个 json:
[{"id":1,"text":"根节点","children":[{"id":2,"text":"子节点 1","children":true},{"id":3,"text":"子节点 2"}]}]
我创建了自己的 url,它返回完全相同的 json。我的问题是,使用我的 url,它提供了一个树节点,我的 json 字符串作为我附加的图像的节点名称。据我所知,两个 url 都返回相同的 json 字符串并且只是原始文本,但我不明白为什么它错误地解释了我的。谁能帮我理解我做错了什么?
$(function() {
$('#jstree').jstree({
'core' : {
'data' : {
"url" : "/_content/data/table/companytree.aspx/?lazy","data" : function (node) {
return { "id" : node.id };
}
}
}
});
});
解决方法
我唯一能想到的,当服务器返回它需要有响应头的数据时。请检查这个,我认为这就是问题所在,我觉得休息很好。
content-type: application/json; charset=utf-8
你也可以尝试一次,添加 contentType,我没有完全使用这个结构,但通常这会告诉返回类型是 json incase 不在 header 中。
$(function() {
$('#jstree').jstree({
'core' : {
'data' : {
"url" : "/_content/data/table/companytree.aspx/?lazy","contentType": "application/json; charset=utf-8","data" : function (node) {
return { "id" : node.id };
}
}
}
});
});
,
我将此代码添加到我的 aspx 代码中,该代码获取数据以发回页面。我没有将 json 发布到页面,而是返回一个 http 响应。它对我有用,但很想知道为什么演示方法没有。我不知道内容类型可能是罪魁祸首。
Response.ContentType = "application/json"
Response.ContentEncoding = Encoding.UTF8
Dim bom As Byte() = Encoding.UTF8.GetPreamble()
Response.BinaryWrite(bom)
Response.BinaryWrite(Encoding.UTF8.GetBytes(PageDataRecords))
Response.Flush()
Response.End()