I have a tree, to be displayed on the screen, which is basically a JSON object. I want to send this JSON object to the Java class as a request parameter.
Following is a sample of the JSON object:
// Root node
var rootNode = { Content: "Root", Nodes:[] };
// First Level
rootNode.Nodes[0] = { Content: "Employee Code", navigationType: "0"};
rootNode.Nodes[1] = { Content: "Problem Area", navigationType: "1" };
// Second Level
rootNode.Nodes[1].Nodes = [{ Content : "ACC-HO", Collapsed: true},
{ Content : "ACC-SALES" },
{ Content : "BUSI. HEAD", ToolTip: "Click ME!" },
{ Content : "CEO"}
];
Now the plugin, which creates this tree out of the JSON object, stores the reference of the Parent Node in each of the nodes. Hence this results in a circular reference where a node stores reference of its children and each child node stores the reference of its Parent
Due to this I am unable to use JSON.stringify(rootNode)
I want to send the rootNode object as a request parameter using Ajax. I have done the following:
$.ajax({
type : "POST",
url : "treeDemo_!saveTree",
data: {treeObject: JSON.stringify(rootNode) },
success : function(resp) {
alert('success');
}
});
But as I mentioned, JSON.stringify doesnt work in my case. So can anyone suggest an alternative way to send the JSON object as a request parameter which I can receive in the Java code and process further?
A solution, if you can’t clean the plugin, is to clone the rootNode object and in the process don’t copy the ‘Parent’ property :
The
serializableRootNodedoesn’t contain the ‘Parent’ properties.Adapt the code if the parent reference isn’t called exactly ‘Parent’.