开发者

Trouble Passing JSON Stringified Array to PageMethod

I am having trouble passing a JSON stringified array to a PageMethod

[{
    "StartDate": "3/1/2011",
    "EndDate": "3/31/2011",
    "UserId": "8",
    "DdlViewSelectedValue": "zzz#",
    "DdlViewSelectedItem": "zzz",
    "DdlOrgSelectedValue": "8"
}, {
    "StartDate": "3/1/2011",
    "EndDate": "3/31/2011",
    "UserId": "9",
    "DdlViewSelectedValue": "zzz#",
    "DdlViewSelectedItem": "zzz",
    "DdlOrgSelectedValue": "8"
}, {
    "StartDate": "3/1/2011",
    "EndDate": "3/31/2011",
    "UserId": 开发者_运维问答"5",
    "DdlViewSelectedValue": "zzz#",
    "DdlViewSelectedItem": "zzz",
    "DdlOrgSelectedValue": "8"
}, {
    "StartDate": "3/1/2011",
    "EndDate": "3/31/2011",
    "UserId": "13",
    "DdlViewSelectedValue": "zzz#",
    "DdlViewSelectedItem": "zzz",
    "DdlOrgSelectedValue": "8"
}, {
    "StartDate": "3/1/2011",
    "EndDate": "3/31/2011",
    "UserId": "6",
    "DdlViewSelectedValue": "zzz#",
    "DdlViewSelectedItem": "zzz",
    "DdlOrgSelectedValue": "8"
}, {
    "StartDate": "3/1/2011",
    "EndDate": "3/31/2011",
    "UserId": "11",
    "DdlViewSelectedValue": "zzz#",
    "DdlViewSelectedItem": "zzz",
    "DdlOrgSelectedValue": "8"
}]

When I get to this ajax request, 'jsonText' contains the data listed above

    function GetUserSchedules() {           
        var jsonText = $.toJSON(arrParams);
        $.ajax({
            type: "POST",
            url: "/myurl/jquery.aspx/GenerateUserSchedules",
            data: "{" + jsonText + "}",
            contentType: "application/json",
            dataType: "json",
            success: AjaxSucceeded
            ,
            error: AjaxFailed
        });
    }

The Pagemethod:

    [System.Web.Script.Services.ScriptMethod]
    [System.Web.Services.WebMethod]
    public static void GenerateUserSchedules(Data[] data)
    {
    //do stuff; will return data but for now, just keeping it like this
            System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();       
}

The DataClass:

[Serializable]
public class Data
{
    public DateTime StartDate { get; set; }
    public DateTime EndDate { get; set; }
    public int UserID { get; set; }
    public string ViewSelectedValue { get; set; }
    public string ViewSelectedItem { get; set; }
    public string OrgSelectedValue { get; set; }
}

Every time the ajax request is sent the error function executes. What am I doing wrong?


That's a common issue with dates. The JavaScriptSerializer expects dates in the following format in order to parse them successfully:

{
    "StartDate": "\/Date(983401200000)\/",
    "EndDate": "\/Date(985989600000)\/",
    "UserId": "8",
    "DdlViewSelectedValue": "zzz#",
    "DdlViewSelectedItem": "zzz",
    "DdlOrgSelectedValue": "8"
}

where 983401200000 represents the number of milliseconds since January 1, 1970 in Universal Coordinated Time (UTC).

Quote from the documentation:

Date object, represented in JSON as "\/Date(number of ticks)\/". The number of ticks is a positive or negative long value that indicates the number of ticks (milliseconds) that have elapsed since midnight 01 January, 1970 UTC.

The maximum supported date value is MaxValue (12/31/9999 11:59:59 PM) and the minimum supported date value is MinValue (1/1/0001 12:00:00 AM).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜