2013-07-19 17 views
0

我使用JSTree插件来显示部门狭窄。 Serverside(asp.net 3.5)效果很好,我得到JSON对象。jsTree 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字符串,通过返回的WebMethod是:

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

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

    ] 
} 

如果我这个字符串复制并粘贴到:

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

我得到正确的结果。 请求帮助,不能得到我做错了什么。

回答

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 
     }); 
    }); 

}); 
+1

我得到错误:未捕获的异常:既不提供数据,也不AJAX设置。在我的萤火虫。但你的建议给了我正确的方向:“json_data”:{“data”:[JSON.parse(msg.d)]}, – ailmcm