【问题标题】:Webmethod is not fired by jsTreejsTree不会触发Webmethod
【发布时间】:2015-01-30 18:31:58
【问题描述】:

我正在尝试从 jsTree 调用 web 方法,但无法调用它。有人可以帮我解决这个问题吗?

我的jsTree函数是:-

 $('#tree').jstree({
    "json_data": {
        "ajax": {
            "type": "POST",
            "dataType": "json",
            "async": true,
            "contentType": "application/json;",
            "opts": {
                "method": "POST",
                "url": "../../SurveyReport/Metrics.aspx/GetAllNodes11"
            },
            "url": "../../SurveyReport/Metrics.aspx/GetAllNodes11",
            "data": function (node) {
                if (node == -1) {
                    return '{ "operation" : "get_children", "id" : -1 }';
                }
                else {
                    //get the children for this node
                    return '{ "operation" : "get_children", "id" : ' + $(node).attr("id") + ' }';
                }
            },
            "success": function (retval) {
                alert('Success')
                return retval.d;
            },
            "error": function (r) {
                alert(r.attr);
                alert('error');
            }
        }
    },
    "plugins": ["themes", "json_data"]
});

而web方法和数据文件是:-

 [WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static List<G_JSTree> GetAllNodes11(string id)
{
    if (id != "-1") //-1 means initial load else async loading of children
    {
        if (id == "10")
            //Add 3 children to parent node with id=10.
            return AddChildNodes(10, 3, "xxxx");
        else
            return new List<G_JSTree>();
    }
    List<G_JSTree> G_JSTreeArray = new List<G_JSTree>();

    //Creating the JsTree data
    //In live scenarios this will come from db or Web Service
    //Add 5 root nodes
    G_JSTreeArray.AddRange(AddChildNodes(0, 5, ""));

    //Add 4 children to 3rd root node
    //The third node has id=30
    //The child nodes will have ids like 301,302,303,304
    G_JSTreeArray[3].children = (AddChildNodes(30, 4, G_JSTreeArray[3].data)).ToArray();

    //Add 5 children to level1 Node at id=302
    G_JSTreeArray[3].children[1].children = (AddChildNodes(302, 4, G_JSTreeArray[3].children[1].data)).ToArray();

    return G_JSTreeArray;
}


private static List<G_JSTree> AddChildNodes(int _ParentID, int NumOfChildren, string ParentName)
{
    List<G_JSTree> G_JSTreeArray = new List<G_JSTree>();
    int n = 10;
    for (int i = 0; i < NumOfChildren; i++)
    {
        int CurrChildId = (_ParentID == 0) ? n : ((_ParentID * 10) + i);
        G_JSTree _G_JSTree = new G_JSTree();
        _G_JSTree.data = (_ParentID == 0) ? "root" + "-Child" + i.ToString() : ParentName + CurrChildId.ToString() + i.ToString();
        _G_JSTree.state = "closed";  //For async to work
        _G_JSTree.IdServerUse = CurrChildId;
        _G_JSTree.children = null;
        _G_JSTree.attr = new G_JsTreeAttribute { id = CurrChildId.ToString(), selected = false };
        G_JSTreeArray.Add(_G_JSTree);
        n = n + 10;
    }
    return G_JSTreeArray;
}

public class G_JSTree
{
    public G_JsTreeAttribute attr;
    public G_JSTree[] children;
    public string data
    {
        get;
        set;
    }
    public int IdServerUse
    {
        get;
        set;
    }
    public string icons
    {
        get;
        set;
    }
    public string state
    {
        get;
        set;
    }
}

public class G_JsTreeAttribute
{
    public string id;
    public bool selected;
}

}

我想从 asp.net 页面中的 web 方法以异步方式加载树。

提前致谢。

【问题讨论】:

  • 你现在的代码有什么具体问题?
  • 感谢您的回复,上面的代码没有调用url中写的webmethod。

标签: javascript jstree jstree-search


【解决方案1】:

我通过添加此完整参考成功使用此代码:

[System.Web.Services.WebMethod]
[System.Web.Script.Services.ScriptMethod(ResponseFormat = System.Web.Script.Services.ResponseFormat.Json)] //ResponseFormat.Json)]
      public static List<GG_JSTree> GetAllNodes11(string id)
        {......}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-12
    相关资源
    最近更新 更多