0

我正在制作一个 ExtJS 树面板以从 .NET Web 服务的 JSON 响应中加载它。似乎我得到了正确的 json 响应,因为当我直接应用响应来存储它时,它会显示树,任何人都可以帮助我在哪里做错了。以下是我的代码:

Ext.onReady(function () {
    var mystore = Ext.create('Ext.data.TreeStore',
    {
        autoLoad: true,
        root: {
            text: 'Root',
            expanded: true//,
            //id: 'ID'
        },
        proxy: {
            type: 'ajax',
            url: 'FirstService.asmx/GetTreeNodes',
            reader:
            {
                type: 'json'
            }
        }
    });

    Ext.create('Ext.tree.Panel', {
        title: 'Simple Tree',
        width: 200,
        height: 300,
        useArrows: true,
        rootVisible: false,
        store: mystore,
        renderTo: 'mydiv'
    });
});

我的 websrevice 代码是这样的:

 public class TreeNode
{
    public int ID { get; set; }
    public string text { get; set; }
    public bool leaf { get; set; }
    public List<TreeNode> children { get; set; }
}


[WebMethod(EnableSession = true)]
[ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = false, XmlSerializeString = false)]
public void GetTreeNodes()
{
    List<TreeNode> list = new List<TreeNode>();

    for (int i = 1; i <= 4; i++)
    {
        TreeNode parent = new TreeNode();
        parent.ID = i;
        parent.text = "Nodes." + i;
        parent.leaf = false;
        parent.children = new List<TreeNode>();

        for (int j = 1; j <= 2; j++)
        {
            TreeNode child = new TreeNode();
            child.ID = i * 10 + j;
            child.text = "Nodes." + child.ID;
            child.leaf = true;
            parent.children.Add(child);
        }

        list.Add(parent);
    }
    string jsonResult = new JavaScriptSerializer().Serialize(list.ToList());
    //{"success": true,"root":[{"ID":1,"text":"Nodes.1","leaf":false,"children":[{"ID":11,"text":"Nodes.11","leaf":true,"children":null},{"ID":12,"text":"Nodes.12","leaf":true,"children":null}]},{"ID":2,"text":"Nodes.2","leaf":false,"children":[{"ID":21,"text":"Nodes.21","leaf":true,"children":null},{"ID":22,"text":"Nodes.22","leaf":true,"children":null}]},{"ID":3,"text":"Nodes.3","leaf":false,"children":[{"ID":31,"text":"Nodes.31","leaf":true,"children":null},{"ID":32,"text":"Nodes.32","leaf":true,"children":null}]},{"ID":4,"text":"Nodes.4","leaf":false,"children":[{"ID":41,"text":"Nodes.41","leaf":true,"children":null},{"ID":42,"text":"Nodes.42","leaf":true,"children":null}]}]}
    //{ root: { expanded: true, children: 
    jsonResult = "{ root: { expanded: true, children: " + jsonResult + "}}";
    System.Web.HttpContext.Current.Response.ContentType = "application/json";
    System.Web.HttpContext.Current.Response.Write(jsonResult);
}

Myjson 响应就像,我在 FF consol 窗口中看到的

 { root: { expanded: true, children: [{"ID":1,"text":"Nodes.1","leaf":false,"children":[{"ID":11,"text":"Nodes.11","leaf":true,"children":null},{"ID":12,"text":"Nodes.12","leaf":true,"children":null}]},{"ID":2,"text":"Nodes.2","leaf":false,"children":[{"ID":21,"text":"Nodes.21","leaf":true,"children":null},{"ID":22,"text":"Nodes.22","leaf":true,"children":null}]},{"ID":3,"text":"Nodes.3","leaf":false,"children":[{"ID":31,"text":"Nodes.31","leaf":true,"children":null},{"ID":32,"text":"Nodes.32","leaf":true,"children":null}]},{"ID":4,"text":"Nodes.4","leaf":false,"children":[{"ID":41,"text":"Nodes.41","leaf":true,"children":null},{"ID":42,"text":"Nodes.42","leaf":true,"children":null}]}]}}

如果我直接在商店中使用此响应它会加载树,请在这里帮助我。谢谢

4

0 回答 0