I'm really confused on how to use this with Ajax/Webmethods. I'm sure I'm missing some little detail but I've been staring at this for so long, I'm not getting anywhere.
I am trying to populate a jsTree from a webmethod as many people I've seen do.
I call a webmethod populateTree with 2 parameters. (I'm not using the parameters yet since I can't even get this to work. Ideally, the parameters is the name of the object calling the method and the node it wants)
WebMethod:
[WebMethod]
public static string populateTree(string imgID, int node)
{
string nodevalues = "";
nodevalues = nodevalues + "{\"id\" : \"ajson1\", \"parent\" : \"#\", \"text\" : \"Commodity\"},";
nodevalues = nodevalues + "{\"data\" : \"2\", \"parent\" : \"#\", \"text\" : \"Control Station\"},";
nodevalues = nodevalues + "{\"data\" : \"3\", \"parent\" : \"#\", \"text\" : \"Build Plan Projects\"},";
return nodevalues;
}
Here is my ajax:
function getRootNode(thisID, node) {
$.ajax({
type: "POST",
url: "jsonPlayground.aspx/populateTree",
data: "{'imgID':'" + thisID + "', 'node': '" + node + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
showtree1(msg.d);
},
error: function () {
alert('Something happened');
}
})
}
And my jsTree function:
function showtree1(nodes) {
$("#jstree_demo_div").jstree({
"core" : {
"data": [
nodes
// { "id": "ajson1", "parent": "#", "text": "Simple root node" },
//{ "id": "ajson2", "parent": "#", "text": "Root node 2" },
//{ "id": "ajson3", "parent": "ajson2", "text": "Child 1" },
//{ "id": "ajson4", "parent": "ajson2", "text": "Child 2" },
]
},
"plugins" : [ "themes", "json_data" ]
});
}
Notice that the commented out format is the exact format that I'm passing to 'nodes'. I've hardcoded it instead of passing the string and it works fine.
What am I missing?
HELP!
Thanks!
Cathryn