这是我在 Default.aspx 中的代码:
$(function() {
var dataSource = {};
$("#MainTree,#SubTree").jstree({
"json_data": {
"ajax":{
type: "POST",
async: true,
url: "Default.aspx/GetJson",
contentType: "application/json; charset=utf-8",
dataType: "json",
cache: false,
success: function(msg) {
dataSource = msg;
},
error: function(err) {
alert(err);
},
data: dataSource,
},
},
"plugins": ["themes", "json_data", "ui", "dnd"]
});
});
这是 Default.aspx.cs 中的 GetJson 方法:
[WebGet(ResponseFormat = WebMessageFormat.Json)]
[System.Web.Services.WebMethod]
public static 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);
}
编辑:这是我检查 GetJson 方法响应时的结果:{"d":"[{\"EmpId\":25,\"Name\":\"Rk\",\"Address\":\"Gurgaon \",\"日期\":\"\/日期(1372999726975)\/\"},{\"EmpId\":50,\"姓名\":\"Sachin\",\"地址\": \"Noida\",\"日期\":\"\/Date(1372999726975)\/\"},{\"EmpId\":10,\"姓名\":\"Nitin\",\"地址\":\"Noida\",\"Date\":\"\/Date(1372999726975)\/\"},{\"EmpId\":21,\"Name\":\"Aditya\", \"地址\":\"Meerut\",\"日期\":\"\/日期(1372999726975)\/\"},{\"EmpId\":100,\"姓名\":\"Mohan \",\"地址\":\"班格洛尔\",\"日期\":\"\/日期(1372999726975)\/\"}]"}
结果什么都没有..它只是突然出现“..Loading”然后返回空白页..请帮我说明这里的问题是什么..非常感谢。