2013-07-05 23 views
0

这是我在Default.aspx的代码:无法从方法的getJSON获取数据

$(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"] 
     }); 
    }); 

这里是的getJSON方法Default.aspx.cs:

[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,\ “名称\”:\ “萨钦\”,\ “地址\”:\”诺伊达\”,\ “日期\”:\ “\ /日期(1372999726975)\/\”},{\ “EMPID \”:10 \ “名称\”:\ “尼廷\”,\ “地址\” :\ “诺伊达\”,\ “日期\”:\ “\ /日期(1372999726975)\/\”},{\ “EMPID \”:21 \ “名称\”:\ “阿迪亚\”,\“地址\ “:\” 密拉特\”,\ “日期\”:\ “\ /日期(1372999726975)\/\”},{\ “EMPID \”:100,\ “名称\”:\ “磨憨\”,\ “Address \”:\“Banglore \”,\“Date \”:\“\/Date(1372999726975)\/\”}]“}

而结果是没有任何内容。加载“闪烁,然后它返回空白页..请帮助我显示这里的问题是..谢谢很多。

回答

0

看来你还没有正确阅读文档,所以我建议你先做。

当使用json_data插件,则需要按照基本的结构如下图所示,可以发现here,这意味着需要在以下格式提供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在成功的功能是从你的代码失踪。

更多的例子可以发现here

请通过您下次使用任何插件的文档。

+0

这是如此伟大..谢谢这么多@无.. –

0

直接在浏览器中键入http://Default.aspx/GetJson,并检查是否有正确的数据。

其他两个位置,您可以添加调试代码是

   success: function(msg) { 
        dataSource = msg; 
       }, 
       error: function(err) { 
        alert(err); 
       } 

添加断点和调试的JavaScript。

+0

请参阅我上面的编辑..我不知道为什么我不能在2个断点调试你显示我..也许它通过这些点步骤.. –

0

响应被封装在名为“d”的属性中。而不是datasource = msg你应该有datasource = msg.d

+0

是...我已经改变了它..它仍然没有什么...像以前发生的事情 –