将 json 数组传递给 WebMethod

Pass json array to WebMethod

我试图将一个对象作为字符串传递给 json 数组,但我得到了 Internal Server Error.

方法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>

首先,您不需要 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 中要做的一些更改:

  • dataType 设置为 json
  • 字符串化你的 data

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

ViewCharts 中的一些变化:

  • 将参数 text 设置为 List<ProcessDetail>

    public static string GetDurations(List<ProcessDetail> processDetails)
    

然后您可以直接访问列表中的 ProcessDetail 个对象:

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