jsTree JSON解析问题

时间:2013-07-19 10:48:50

标签: jquery asp.net json jstree

我使用JSTree插件来显示部门限制。 Serverside(asp.net 3.5)运行良好,我得到了JSON对象。

但是当我尝试时:

$(document).ready(function () {
    $('#btntst').click(function () {
        $('#mainDiv').html('wait for data');
        $.ajax({
            type: 'POST',
            url: '_layouts/GridView/ApplicationPage1.aspx/getTable',
            contentType: "application/json; charset=utf-8",
            dataType: 'json',
            data: "{}",
            success: function (msg) {
                $('#jsTreeContainer').jstree({
                    "json_data": {
                        "data": [msg.d]
                    }
                    , "plugins": ["themes", "json_data"]
                });
            }
            , timeout: 60000
        });
    });

});

我只获得一个包含所有JSON字符串的节点 webmethod返回的JSON字符串是:

{
  'data': 'department001',
  'attr': {
    'id': 'nodeid1773'
  },
  'children': [

  ]
},
{
  'data': 'department001',
  'attr': {
    'id': 'nodeid1779'
  },
  'children': [

  ]
}

如果我将此字符串复制粘贴到:

"json_data": {"data" : [...] }

我得到了正确的结果。 请求帮助,不能解决我做错的事。

1 个答案:

答案 0 :(得分:1)

您的脚本正在查找类型为json_data的JSON对象,但正常响应仅为data。看看这些变化是否有效:

$(document).ready(function () {
    $('#btntst').click(function () {
        $('#mainDiv').html('wait for data');
        $.ajax({
            type: 'POST',
            url: '_layouts/GridView/ApplicationPage1.aspx/getTable',
            contentType: "application/json; charset=utf-8",
            dataType: 'json',
            data: "{}",
            success: function (msg) {
                $('#jsTreeContainer').jstree({
                    "json_data": [msg.d],
                    "plugins": ["themes", "json_data"]
                });
            }
            , timeout: 60000
        });
    });

});
相关问题