2013-07-05 24 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

是...我已經改變了它..它仍然沒有什麼...像以前發生的事情 –