C# Can';无法从GetJson方法获取数据
以下是我在Default.aspx中的代码:C# Can';无法从GetJson方法获取数据,c#,asp.net,jstree,C#,Asp.net,Jstree,以下是我在Default.aspx中的代码: $(function() { var dataSource = {}; $("#MainTree,#SubTree").jstree({ "json_data": { "ajax":{ type: "POST", async: true, url
$(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);
}
[WebGet(ResponseFormat=WebMessageFormat.Json)]
[System.Web.Services.WebMethod]
公共静态字符串GetJson()
{
System.Web.Script.Serialization.JavaScriptSerializer serializer=新的System.Web.Script.Serialization.JavaScriptSerializer();
列表行=新列表();
字典行=空;
DataTable dtEmployee=新DataTable();
Add(“EmpId”,typeof(int));
dtEmployee.Columns.Add(“Name”,typeof(string));
dtEmployee.Columns.Add(“地址”,typeof(字符串));
添加(“日期”,typeof(DateTime));
//
//这里我们添加五个数据行。
//
添加(25,“Rk”,“Gurgaon”,DateTime.Now);
添加(50,“Sachin”,“Noida”,DateTime.Now);
添加(10,“Nitin”,“Noida”,DateTime.Now);
添加(21,“Aditya”,“Meerut”,DateTime.Now);
添加(100,“Mohan”,“Banglore”,DateTime.Now);
foreach(dtEmployee.Rows中的数据行dr)
{
行=新字典();
foreach(dtEmployee.Columns中的数据列col)
{
行添加(列名称,dr[col]);
}
行。添加(行);
}
返回序列化程序。序列化(行);
}
编辑:
这是我检查GetJson方法respone时得到的结果:
“d”d:“,,,“日期:“:,,,,“日期:”,,,,,,“日期:,,,,,,“日期:,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,{\'EmpId\':21,\'Name\':\'Aditya\',\'Address\':\'Meerut\',\'日期:“:\”\/Date(1372999726975)\/“},{“EmpId\”:100,““Name\”:“Mohan\”,““Address\”:“Banglore\”,““Date\”:\“\/Date(1372999726975)\/”}”
结果什么都没有。它只是出现了。快速加载,然后返回空白页。请帮我说明问题所在。非常感谢。键入http://Default.aspx/GetJson 直接在浏览器中查看是否有正确的数据 可以添加调试代码的其他两个位置是
success: function(msg) {
dataSource = msg;
},
error: function(err) {
alert(err);
}
添加断点并调试javascript。响应封装在名为“d”的属性中。您不应该使用
datasource=msg
而是应该使用datasource=msg.d
您似乎没有正确阅读文档,因此我建议您先这样做
当您使用json_data插件时,您需要遵循如下所示的基本结构,这意味着您需要以以下格式提供json数据:
{
"data" : "node_title",
// omit `attr` if not needed; the `attr` object gets passed to the jQuery `attr` function
"attr" : { "id" : "node_identificator", "some-other-attribute" : "attribute_value" },
// `state` and `children` are only used for NON-leaf nodes
"state" : "closed", // or "open", defaults to "closed"
"children" : [ /* an array of child nodes objects */ ]
}
考虑到响应结构,您需要有如下所示的服务器端类:
public class Emp
{
public EmpAttribute attr { get; set; }
public string data { get; set; }
}
public class EmpAttribute
{
public string id;
public bool selected;
}
您的pagemethod应该如下所示:
[WebGet(ResponseFormat = WebMessageFormat.Json)]
[System.Web.Services.WebMethod]
public static List<Emp> GetJson()
{
List<Emp> empTreeArray = new List<Emp>();
Emp emp1 = new Emp()
{
attr = new EmpAttribute(){ id= "25",selected=false},
data = "Nitin-Gurgaon"
};
Emp emp2 = new Emp()
{
attr = new EmpAttribute(){ id="50",selected=false},
data = "Sachin-Noida"
};
empTreeArray.Add(emp1);
empTreeArray.Add(emp2);
return empTreeArray;
}
$(function() {
var dataSource = {};
$("#demo1").jstree({
"json_data": {
"ajax":{
"type": "POST",
"url": "Default2.aspx/GetJson",
"contentType": "application/json; charset=utf-8",
"dataType": "json",
success: function(msg) {
return msg.d;
},
error: function(err) {
alert(err);
}
}
},
"plugins": ["themes", "json_data", "ui", "dnd"]
});
});
请注意,代码中缺少成功函数中的返回msg.d
可以找到更多的例子
请查看您下次使用的任何插件的文档。请查看我的上面的编辑..我不知道为什么我不能在您显示给我的两个断点处调试..也许它会逐步通过这些点..是的..我已经更改了..但仍然没有得到任何..像以前发生的那样