将数据源转换为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来从datasource获取json_data。它总是显示“..Loading”..我正在使用jsTree和.net框架2.0 ..请帮我找到解决方案。谢谢
答案 0 :(得分:1)
使用Default.aspx/GetJson
代替Default.aspx.cs/GetJson
。
答案 1 :(得分:0)
除了更改文件名(如@Alex Filipovici所述),您的方法必须是static
。
[WebMethod]
public static string GetJson()
{
}
EDIT :::
这就是我所做的,并得到了结果:
$(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;
});
});