0

我在将数据源转换为 json_data 时遇到问题。这是我的代码:

在我的 default.aspx.cs 中:

[WebMethod]
public string GetJson()
{
    System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
    List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
    Dictionary<string, object> row = null;

    DataTable dtEmployee = new DataTable();

    dtEmployee.Columns.Add("EmpId", typeof(int));
    dtEmployee.Columns.Add("Name", typeof(string));
    dtEmployee.Columns.Add("Address", typeof(string));
    dtEmployee.Columns.Add("Date", typeof(DateTime));

    //
    // Here we add five DataRows.
    //
    dtEmployee.Rows.Add(25, "Rk", "Gurgaon", DateTime.Now);
    dtEmployee.Rows.Add(50, "Sachin", "Noida", DateTime.Now);
    dtEmployee.Rows.Add(10, "Nitin", "Noida", DateTime.Now);
    dtEmployee.Rows.Add(21, "Aditya", "Meerut", DateTime.Now);
    dtEmployee.Rows.Add(100, "Mohan", "Banglore", DateTime.Now);

    foreach (DataRow dr in dtEmployee.Rows)
    {
        row = new Dictionary<string, object>();
        foreach (DataColumn col in dtEmployee.Columns)
        {
            row.Add(col.ColumnName, dr[col]);
        }
        rows.Add(row);
    }
    return serializer.Serialize(rows);
}      

这是在我的 default.apsx 页面中:

var data2 = { };
    function GetJsonData(callback) {

        $.ajax({
                    type: "POST",
                    async: true,
                    url: "Default.aspx/GetJson",
                    //contentType: "application/json; charset=utf-8",
                    //data: '{name:""}',
                    dataType: "json",
                    cache: false,
                    success: function(msg) {
                        callback(msg);
                    },
                    error: function(err) {
                        alert('error');
                    }
                });
    }
    data2 = GetJsonData();
    $(function() {
        $("#MainTree,#SubTree").jstree({
            "json_data": {
                "data": data2,
            },
            "plugins": ["themes", "json_data", "ui", "dnd"]
        });
    });

当我隐藏“数据”时,它当然不会创建节点。但是现在我想从 default.aspx.cs 中调用 GetJson 方法来从数据源中获取 json_data。它总是显示“..Loading”..我正在使用 jsTree 和 .net framework 2.0..请帮我找出解决方案..谢谢

4

2 回答 2

1

使用Default.aspx/GetJson而不是Default.aspx.cs/GetJson.

于 2013-07-04T09:38:15.037 回答
0

除了更改文件名(如@Alex Filipovici 所述)之外,您的方法还需要是static.

[WebMethod]
public static string GetJson()
{
}

编辑:::

这就是我所做的并得到了结果:

 $(document).ready(function () {

            $("[id$=_btnPostReminder]").click(function () {
                var a = "";
                var remindertext = "";
                var re = "";
                var res = "";
                $.ajax({
                    type: "POST",
                    url: "Default.aspx/GetJson",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success:
        function Ret(response) {
            var Result = response.d
            alert(Result)

            return false;
        },
                    error: function (data, status, jqXHR) {
                        alert(jqXHR);
                    }
                });
                return false;
            });
        });
于 2013-07-04T09:49:11.110 回答