我正在尝试使用ajax将信息推送并发布到Django。在我的模型中,我只使用三个字段:id,parent和text。
当我尝试将信息发回Django时,我遇到了一个错误,因为JSON文件中有额外的字段,其中包含我在模型中没有的信息。
我希望能够从我拥有的JSON数据中只收集字段id,parent和text,以便我可以将它成功发送回Django。
这是我目前的代码:
$('#jstree_demo').jstree({
"core" : {
"animation" : 0,
"check_callback" : true,
"themes" : { "stripes" : true },
'data' : {
"url" : "/snippets/",
"success": function(data){
series = data;
},
"dataType" : "json" // needed only if you do not supply JSON headers
}
},
"plugins" : [ "contextmenu", "dnd", "search", "state", "types", "wholerow" ]
});
});
function get_tree(){
var v = $('#jstree_demo').jstree(true).get_json('#', {flat:true})
var mytext = JSON.stringify(v);
$.ajax({
url:/snippets/,
type:"POST",
data:mytext,
contentType:"application/json; charset=utf-8",
dataType:"json",
})
}
function demo_create() {
var ref = $('#jstree_demo').jstree(true),
sel = ref.get_selected();
if(!sel.length) { return false; }
sel = sel[0];
sel = ref.create_node(sel, {"type":"default"});
if(sel) {
ref.edit(sel);
}
get_tree();
};
function demo_rename() {
var ref = $('#jstree_demo').jstree(true),
sel = ref.get_selected();
if(!sel.length) { return false; }
sel = sel[0];
ref.edit(sel);
get_tree();
};
function demo_delete() {
var ref = $('#jstree_demo').jstree(true),
sel = ref.get_selected();
if(!sel.length) { return false; }
ref.delete_node(sel);
get_tree();
};
这是我从变量“mytext”获得的示例JSON
[{"id":"node_one","text":"say its so","icon":true,"li_attr":{"id":"node_one"},"a_attr":{"href":"#","id":"node_one_anchor"},"state":{"loaded":true,"opened":true,"selected":false,"disabled":false},"data":{},"parent":"#","type":"default"},{"id":"some","text":"Ivan is sleepy","icon":true,"li_attr":{"id":"some"},"a_attr":{"href":"#","id":"some_anchor"},"state":{"loaded":true,"opened":false,"selected":false,"disabled":false},"data":{},"parent":"node_one","type":"default"},{"id":"node_1","text":"Node","icon":true,"li_attr":{"id":"node_1"},"a_attr":{"href":"#","id":"node_1_anchor"},"state":{"loaded":true,"opened":false,"selected":true,"disabled":false},"data":{},"parent":"#","type":"default"},{"id":"j1_10","text":"New node","icon":true,"li_attr":{"id":"j1_10"},"a_attr":{"href":"#"},"state":{"loaded":true,"opened":false,"selected":false,"disabled":false},"data":{},"parent":"node_1","type":"default"},{"id":"node1","text":"test worked","icon":true,"li_attr":{"id":"node1"},"a_attr":{"href":"#","id":"node1_anchor"},"state":{"loaded":true,"opened":false,"selected":false,"disabled":false},"data":{},"parent":"#","type":"default"},{"id":"j1_1","text":"New node","icon":true,"li_attr":{"id":"j1_1"},"a_attr":{"href":"#"},"state":{"loaded":true,"opened":false,"selected":false,"disabled":false},"data":{},"parent":"node1","type":"default"},{"id":"node2","text":"test worked 2","icon":true,"li_attr":{"id":"node2"},"a_attr":{"href":"#","id":"node2_anchor"},"state":{"loaded":true,"opened":false,"selected":false,"disabled":false},"data":{},"parent":"#","type":"default"},{"id":"j1_9","text":"New node","icon":true,"li_attr":{"id":"j1_9"},"a_attr":{"href":"#"},"state":{"loaded":true,"opened":false,"selected":false,"disabled":false},"data":{},"parent":"node2","type":"default"},{"id":"node3","text":"test worked yay","icon":true,"li_attr":{"id":"node3"},"a_attr":{"href":"#","id":"node3_anchor"},"state":{"loaded":true,"opened":false,"selected":false,"disabled":false},"data":{},"parent":"#","type":"default"},{"id":"j1_2","text":"New node","icon":true,"li_attr":{"id":"j1_2"},"a_attr":{"href":"#"},"state":{"loaded":true,"opened":false,"selected":false,"disabled":false},"data":{},"parent":"node3","type":"default"},{"id":"j1_7","text":"New node","icon":true,"li_attr":{"id":"j1_7"},"a_attr":{"href":"#"},"state":{"loaded":true,"opened":false,"selected":false,"disabled":false},"data":{},"parent":"j1_2","type":"default"},{"id":"j1_8","text":"New node","icon":true,"li_attr":{"id":"j1_8"},"a_attr":{"href":"#"},"state":{"loaded":true,"opened":false,"selected":false,"disabled":false},"data":{},"parent":"j1_2","type":"default"},{"id":"j1_3","text":"New node","icon":true,"li_attr":{"id":"j1_3"},"a_attr":{"href":"#"},"state":{"loaded":true,"opened":true,"selected":false,"disabled":false},"data":{},"parent":"node3","type":"default"},{"id":"j1_4","text":"New node","icon":true,"li_attr":{"id":"j1_4"},"a_attr":{"href":"#"},"state":{"loaded":true,"opened":false,"selected":false,"disabled":false},"data":{},"parent":"j1_3","type":"default"},{"id":"j1_6","text":"New node","icon":true,"li_attr":{"id":"j1_6"},"a_attr":{"href":"#"},"state":{"loaded":true,"opened":false,"selected":false,"disabled":false},"data":{},"parent":"j1_4","type":"default"},{"id":"j1_5","text":"New node","icon":true,"li_attr":{"id":"j1_5"},"a_attr":{"href":"#"},"state":{"loaded":true,"opened":false,"selected":false,"disabled":false},"data":{},"parent":"j1_3","type":"default"}]
我正在尝试与jsTree协作。非常感谢任何帮助。
答案 0 :(得分:0)
使用var mytext = JSON.parse(v);
代替stringify。
它将返回Json数组对象。现在,您可以将数组中的id,parent和text用于每个temperory变量。
现在对这个新变量进行stringfy并传递数据。
希望这可以帮助您解决问题。