2013-07-04 41 views
0

我在將數據源轉換爲json_data時遇到了問題。這裏是我的代碼:加載數據源時出錯,並將其轉換爲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方法從數據源獲取json_data。它總是顯示「..Loading」..我使用jsTree和只是.NET框架2.0 ..請幫我找出解決方案爲此..謝謝

+0

你在CS任何異常? –

+0

Follow @AlexFilipovici post –

+0

@kostasch。不,我沒有任何異常。 –

回答

1

使用Default.aspx/GetJson而不是Default.aspx.cs/GetJson

+0

哦..我已經改變了它,也添加了「靜態」的方法..但它沒有得到任何結果.. –

0

除了更改文件名(如@Alex Filipovici提到的),您的方法需要爲static

[WebMethod] 
public static string GetJson() 
{ 
} 

編輯:::

這是我做的,並得到返回結果:

$(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; 
      }); 
     }); 
+0

我改變了..但它沒有得到任何結果.. –

+0

請參閱我的編輯。 – Zaki

+0

我已經嘗試過,仍然得到「.. loading」..請看我編輯的代碼 –

相關問題