【问题标题】:jsTree JSON parse issuejsTree JSON解析问题
【发布时间】:2013-07-19 10:48:49
【问题描述】:

我使用 JSTree 插件来显示部门限制。 服务器端(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 字符串的节点。
JSON-string,webmethod返回的是:

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

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

  ]
}

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

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

我得到正确的结果。 请帮忙,不明白我在做什么错。

【问题讨论】:

    标签: jquery asp.net json jstree


    【解决方案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
            });
        });
    
    });
    

    【讨论】:

    • 我收到错误:未捕获的异常:未提供数据和 ajax 设置。在我的萤火虫中。但是您的建议给了我正确的方向: "json_data": {"data": [JSON.parse(msg.d)]},
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-29
    • 2019-01-30
    相关资源
    最近更新 更多