As for question 1 (I can't talk to Q2), I got a web service to pass JSON back to the jsTree plugin. Although I admit that WCF and REST are more modern approaches, and undoubtedly better in the long run, we still use asmx services and they do their job. But it was not easy: I spent some time trying to get the exact syntax for working in JS so that jsTree gets its data object from the ASP.NET web service. As the OP points out in its solution, the problem is not so much in my JS and the plugin wiring, but in the fact that I had JSON data that my web service returned. It looked like JSON, but it was a string representation of a simplified JSON object.
To fix this, I had to deserialize and serialize my json string (I understood this from fooobar.com/questions/122476 / ... ) and the resulting object was successfully used by jsTree, Here is my web service:
Imports System.Web.Script.Serialization Public Function GetJsonData() As String Dim jss As JavaScriptSerializer = New JavaScriptSerializer ' IMPORTANT: do not use single quotes ' in JSON string, use "" Dim jsonData As String = "" & _ "{ ""text"": ""CONTACTS"", ""children"": [ { ""text"": ""CUSTOMER"", ""column"": ""CLASS"", ""children"": [ { ""text"": ""Excelo Communications"", ""column"": ""COMPANY"", ""children"": [{ ""text"": ""Fred Shorts"", ""column"": ""CONTACT"" }] } ] }, { ""text"": ""USER"", ""column"": ""CLASS"" } ] }" Dim o As Object = Nothing Try ' deserialize the JSON into an Object, ' shout out to: /questions/122476/deserializing-json-in-visual-basic/747174#747174 o = jss.Deserialize(Of Object)(jsonData) o = jss.Serialize(o) Context.Response.Clear() Context.Response.ContentType = "application/json; charset=utf-8" Catch ex As Exception // log something End Try Return o End Function
On the client, I initialized jsTree in a script as follows:
$(document).ready(function () { var sURL = "../dataIO.asmx/GetJsonData"; var dataIn = ""; $.ajax({ async: true, type: "POST", url: sURL, dataType: "json", data: dataIn, contentType: "application/json; charset=utf-8", success: function (data) { console.log("data obj:" + data); createJSTrees(data); }, error: function (XMLHttpRequest, textStatus, errorThrown) { console.log(XMLHttpRequest.statusText + "(status=" + XMLHttpRequest.status + "): " + XMLHttpRequest.responseText); } }); }); function createJSTrees(jsonData) { $("#jstree_dataNav").jstree({ "core": { "data": jsonData } }); $("#jstree_dataNav").on("changed.jstree", function (e, data) { console.log(data.selected); }); } <div id="jstree_dataNav"></div>
jsTree has a somewhat alternative syntax, while you are calling a web service in the core.data section, but I could not get this to work. Instead, I call my web service through ajax, and then pass the JSON data object to the function that initializes the jsTree plugin, and jsTree just used the object passed inside the data :.