Passing javascript object to ajax
Pergunta
I'm trying to use dynatree, and it can be initialized like this:
var treeData =
[
{title: "group1", key:"182" },
{title: "group2", key:"181" },
{title: "group3", key:"189" , isFolder:true, children:
[
{title: "group3_1", key:"301" }
]
}
];
$(".groupContainer").dynatree({
checkbox: false,
selectMode: 2,
children: treeData
});
Notice that the objects in the treeData
have values referenced by names that are not quoted.
Now, I want to do this:
var treeData = [];
$.getJSON('/getTreeData.do', function(data){
treeData = data;
$(".groupContainer").dynatree({
checkbox: false,
selectMode: 2,
children: treeData
});
});
However, jquery 1.7.1 (and 1.5.2 and 1.4.2) raise parse errors when I pass the contents of treeData
from getTreeData.do
. I believe this is because the specification for JSON requires that keys and values are strings, and are quoted. But if I quote the keys and values, then dynatree will draw a tree with a single node labeled "null".
Is there a way to do this, or will I have to use dynatree's createNode()
and addNode()
functions a bunch of times after retrieving the tree data?
On a related note, how is a legal javascript object not legal JSON?
Solução
I've changed the response type to "text/plain" and ran javascript's eval()
on the response. Gee, why didn't I think of that before?