1

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?

Bengal
  • 329
  • 4
  • 21

1 Answers1

2

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).

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • That's a useful tip, but I haven't begun to deserialize yet in the page method – Bengal Mar 02 '11 at 18:08
  • @Bengal, yes you haven't but that's done automatically even before your method is called. And because this deserialization to a `Data[]` instance fails the error callback is invoked. You shouldn't serialize/deserialize manually. It's done automatically for input/output parameters. – Darin Dimitrov Mar 02 '11 at 18:09
  • thanks for you feedback. I did change the dates as you suggested, but still no luck. BTW, how is one supposed to convert the dates to the format you describe? – Bengal Mar 02 '11 at 18:34
  • @Bengal - the date issue never occurred to me! One of the answers here shows how to do that. http://stackoverflow.com/questions/1224793/javascript-serialization-of-datetime-in-asp-net-is-not-giving-a-javascript-date-o – Jamie Treworgy Mar 02 '11 at 20:32