none
WCF and JSON Date time RRS feed

  • Question

  • I have a WCF service which returns data in JSON format.
    
    
    the operation contract goes like this
    
    
    [OperationContract]
    [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
    string GetTime(DateTime dt);
    
    
    public string GetTime(DateTime dt)
    {
     return dt.Date.ToString();
    }
    
    
    I am using Jquery ajax library to call the service
    
     $.ajax({
     type: "POST",
     url: "/DataItemsService.svc/web/GetTime",
     data: { "dt": "1994-11-05T08:15:30-05:00"},
     contentType: "application/json; charset=utf-8",
     dataType: "json",
     success: function (data) {
     alert(data.GetTimeResult);
     },
     error: function (XMLHttpRequest, textStatus, errorThrown) {
     debugger;
     alert("Error Occured!");
     }
     });
    
    
    Can some one guide me in passing datetime values
    I tried couple of ways but no luck
    
    { "dt": "1994-11-05T08:15:30-05:00"}
    { "dt": "\/Date(1297293089984-0800)\/"}
    
    getting error saying The server encountered an error processing the request in fiddler response
    
    
    Thursday, February 10, 2011 12:14 AM

Answers

  • $.ajax by default encodes input in application/x-www-form-urlencoded format (even if you say that the contentType is application/json). You can either do the JSON encoding yourself:

    data: "{\"dt\":\"\/Date(1297293089984-0800)\/\"},

    Or use some JSON encoding library (such as the json2.js from www.json.org)

    data: JSON.stringify({dt: "\/Date(1297293089984-0800)\/"}),

     

    • Marked as answer by hobbydotnet Thursday, February 10, 2011 1:17 AM
    Thursday, February 10, 2011 12:55 AM

All replies

  • I have a WCF service which returns data in JSON format.
    
    
    
    the operation contract goes like this
    
    
    
    [OperationContract]
    
    [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
    
    string GetTime(DateTime dt);
    
    
    
    
    
    public string GetTime(DateTime dt)
    
    {
    
     return dt.Date.ToString();
    
    }
    
    
    
    
    
    I am using Jquery ajax library to call the service
    
    
    
    
    
      $.ajax({
    
       type: "POST",
    
       url: "/DataItemsService.svc/web/GetTime",
    
       data: { "dt": "1994-11-05T08:15:30-05:00"},
    
       contentType: "application/json; charset=utf-8",
    
       dataType: "json",
    
       success: function (data) {
    
        alert(data.GetTimeResult);
    
        
    
       },
    
       error: function (XMLHttpRequest, textStatus, errorThrown) {
    
        debugger;
    
        alert("Error Occured!");
    
       }
    
      });
    
    
    
    Can some one guide me in passing datetime values
    
    
    
    I tried couple of ways but no luck
    
    { "dt": "1994-11-05T08:15:30-05:00"}
    
    { "dt": "\/Date(1297293089984-0800)\/"}
    
    getting error saying The server encountered an error processing the request in fiddler response
    
    
    
    
    Wednesday, February 9, 2011 11:21 PM
  • $.ajax by default encodes input in application/x-www-form-urlencoded format (even if you say that the contentType is application/json). You can either do the JSON encoding yourself:

    data: "{\"dt\":\"\/Date(1297293089984-0800)\/\"},

    Or use some JSON encoding library (such as the json2.js from www.json.org)

    data: JSON.stringify({dt: "\/Date(1297293089984-0800)\/"}),

     

    • Marked as answer by hobbydotnet Thursday, February 10, 2011 1:17 AM
    Thursday, February 10, 2011 12:55 AM
  • Thnaks for your quick response, it solved my problem half.

    Can I know how to generate Date(1297293089984-0800) the bold part suppose if I want to pass today's date.

    also do I need to pass Time Zone offset?

    Thursday, February 10, 2011 1:06 AM
  • You don't need to pass the time zone offset; if you do, then the date deserialized in the server will be of kind DateTimeKind.Local. If no offset is present, it will be of kind DateTimeKind.Utc (see section "DateTime wire format at http://msdn.microsoft.com/en-us/library/bb412170.aspx).

    To generate that number, you can call the getTime method on the Date object:

    var now = new Date();
    var dateStringRepresentation = "\/Date(" + now.getTime() + ")\/";

     

    Thursday, February 10, 2011 2:03 AM
  • Thanks very much
    Thursday, February 10, 2011 5:38 AM
  • Try JSON.stringify( { "dt": "\/Date(1297293089984-0800)\/"})

    Thursday, February 10, 2011 9:01 PM