Can't load JSON into a jstree from WCF service (or How to find what's wrong when jstree stays on "loading...")
I am trying to get jStree (jQuery tree control) working in my ASP.NET (C#) page. I'm using a WCF service to return a test string of JSON.
Opening the WCF service URL in the browser works fine - If I go to the URL (http://localhost/website/GetTree) I get the JSON (looks just like opening XML in browser, with syntax highlighting and collapsible tab thingys)
<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">{"attr": { "id": 2, "rel": "default" }, "data": "A node", "children": [ { "data": "Child 1", "attr": { "id": 43, "rel": "document" } }, {"data": "Child 2", "attr": { "id": 25, "rel": "document"} } ] }</string>
jstree itself works fine - if I put the same JSON in "manually" in my document.ready like so:
"json_data": {
"data":
[{
"attr": { "id": 2, "rel": "default" },
"data": "A node",
"children":
[{
"data": "Child 1", "attr": { "id": 43, "rel": "document" }
},{
"data": "Child 2", "attr": { "id": 25, "rel": "document"}
}]},{
"attr": { "id": "li.node.id", "rel": "document" },
"data": {
"title": "Long format demo",
"attr": { "href": "#" }
}}]},
I get the tree appearing with those nodes in it.
But, combining the two:
"json_data": {
"ajax": {
"url": "GetTree",
"data": function (n) {
return {
};
}
}
},
...fails. The jstree shows up as just the word "loading..." and an animated loading gif. And it does hit a breakpoint inside the WCF service method (just like browsing to it does).
A: An idea where I've gone wrong?
B: An idea how I find out what's wrong? jstree documentation was no help, and I can't see any error messages.
Thanks.
Update: solved
Thanks to Mikael Eliasson for pointing out that my service must be returning XML and not JSON.
WCF service code-behind:
// this responseformat bit below fixed it
[WebGet(UriTemplate = "GetTesto", ResponseFormat = WebMessageFormat.Json)]
[OperationContract]
public Testo GetTe开发者_运维百科sto()
{
return new Testo();
}
Might be a stupid question but you have enabled the json_data plugin, right? As akonsu said inspecting the response in a tool like Firebug.
It seems to me that you WCF service is not returning JSON but rather a XML response. I think that is your problem. The best thing is probably to change the service to return JSON. If you can't do that you could add the success function and transform the string to JSON with the function $.parseJSON()
I hope that helps. But again you really should start using Firebug or something similar because there are lot of times when you need to inspect the response or debug javascript.
精彩评论