2017-01-18 71 views
0

我試圖將一個對象傳遞給json數組作爲字符串,但我得到了Internal Server Error將json數組傳遞給WebMethod

方法GetServerTime正在工作。

我想通過對象的數組服務器並將其轉換成的ProcessDetail

public class ProcessDetail 
{ 
    public DateTime? StartDate {get;set;} 
    public DateTime? EndDate {get;set;} 
} 

//Web Methods 
[WebMethod] 
public static string GetServerTime() 
{ 
    return DateTimeHelper.AppDateTime.ToString(CultureInfo.CurrentCulture); 
} 

[WebMethod] 
public static string GetDurations(string text) 
{ 
    List<ProcessDetail> details = DeSerialize<ProcessDetail>(text); 
    return string.Empty; 
} 

public static List<T> DeSerialize<T>(string input) 
{ 
    var serializer = new DataContractJsonSerializer(typeof(List<T>)); 

    using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(input))) 
    { 
     return (List<T>)serializer.ReadObject(ms); 
    } 
} 

// Javascirpt 
<script> 
    function DateChanged() { 
     var grid = document.getElementById('<%= gvProcessMonitoring.ClientID %>'); 
     var servertime = getData('<%= ResolveUrl("ViewCharts.aspx/GetServerTime") %>', {}); 
     alert(servertime); 

     var processDetails = { 
      processDetail: [] 
     }; 

     if (grid.rows.length > 0) { 
      for (var i = 1; i < grid.rows.length; i++) { 
       var txtStartDate = grid.rows[i].getElementsByClassName('grdtxtStartDate')[0]; 
       var txtEndDate = grid.rows[i].getElementsByClassName('grdtxtEndDate')[0]; 

       processDetails.processDetail.push({ 
        "StartDate": "'" + txtStartDate.value + "'", 
        "EndDate": "'" + txtEndDate.value + "'" 
       }); 
      } 

      // This is giving internal server error.I have tried without stringify. 
      var serverData = getData('<%= ResolveUrl("ViewCharts.aspx/GetDurations") %>', JSON.stringify(processDetails)); 
     } 
    } 

    function getData(dataUrl, dataString) { 
     var jsonObject; 

     $.ajax({ 
      type: "POST", 
      contentType: "application/json; charset=utf-8", 
      url: dataUrl, 
      data: dataString, 
      async: false, 
      cache: false, 
      timeout: 30000, 
      success: function (data) { 
       $(".loadingdiv").remove(); 
       jsonObject = data.d; 
      }, 
      error: function (result) { 
       debugger; 
       $(".loadingdiv").remove(); 
       alert("Unexpected error"); 
      } 
     }); 

     return jsonObject; 
    } 
</script> 

回答

2

首先泛型列表,你不需要processDetails對象,你可以簡單地將對象添加到一個數組:

var processDetails = []; 

for (var i = 1; i < grid.rows.length; i++) { 
    // [...] 
    var processDetail = { StartDate: txtStartDate.value, EndDate: txtEndDate.value }; 
    processDetails.push(processDetail); 
} 

然後,傳遞到getData不字符串化陣列:

var serverData = getData('<%= ResolveUrl("ViewCharts.aspx/GetDurations") %>', processDetails); 

一些變化在getData做:

  • dataTypejson
  • 字符串化的data

    dataType: "json", 
    data: JSON.stringify({ processDetails: dataString }) 
    

中的某些變化ViewCharts

  • 設置你的參數textList<ProcessDetail>

    public static string GetDurations(List<ProcessDetail> processDetails) 
    

然後,您可以訪問您的列表直接在ProcessDetail對象:

foreach (ProcessDetail processDetail in processDetails) 
{ 
    // processDetail.StartDate 
    // processDetail.EndDate 
}