无法从 WCF 服务将 JSON 加载到 jstree(或当 jstree 停留在“正在加载...”时如何查找问题所在)
我正在尝试让 jStree(jQuery 树控件)在我的 ASP.NET (C#) 页面中工作。我正在使用 WCF 服务返回 JSON 测试字符串。
在浏览器中打开 WCF 服务 URL 工作正常 - 如果我转到 URL (http://localhost/website/GetTree),我会得到 JSON(看起来就像在浏览器中打开 XML,具有语法)突出显示和可折叠选项卡的东西)
<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 本身工作正常 - 如果我将相同的 JSON “手动”放入我的 document.ready 中,如下所示:
"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": "#" }
}}]},
我会得到其中包含这些节点的树。
但是,将两者结合起来:
"json_data": {
"ajax": {
"url": "GetTree",
"data": function (n) {
return {
};
}
}
},
……失败了。 jstree 仅显示为单词“正在加载...” 和动画加载 gif。它确实命中了 WCF 服务方法内的断点(就像浏览到它一样)。
A:我哪里出错了?
B:如何找出出了什么问题? jstree 文档没有帮助,我看不到任何错误消息。
谢谢。
更新:已解决
感谢 Mikael Eliasson 指出我的服务必须返回 XML 而不是 JSON。
WCF服务代码隐藏:
// this responseformat bit below fixed it
[WebGet(UriTemplate = "GetTesto", ResponseFormat = WebMessageFormat.Json)]
[OperationContract]
public Testo GetTesto()
{
return new Testo();
}
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 GetTesto()
{
return new Testo();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可能是一个愚蠢的问题,但您已经启用了 json_data 插件,对吗?正如 akonsu 所说,在 Firebug 等工具中检查响应。
在我看来,您的 WCF 服务没有返回 JSON,而是返回 XML 响应。我认为这是你的问题。最好的办法可能是将服务更改为返回 JSON。如果您不能这样做,您可以添加 success 函数并使用函数 $.parseJSON() 将字符串转换为 JSON,
我希望有所帮助。但你真的应该开始使用 Firebug 或类似的东西,因为很多时候你需要检查响应或调试 javascript。
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.