我在将数据源转换为 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..请帮我找出解决方案..谢谢