How to pass a Date() object to a C# WebMethod?

itnifl
itnifl used Ask the Experts™
on
function addBooking(logonName, startTime, endTime) {
    var hours = Math.abs(startTime - endTime) / 36e5;
    $.ajax({
        type: "POST",
        url: "/Sections/booker.aspx/BookTime",
        data: "{bookingDate= '" + startTime + "', durationHours=" + hours + ", logonName='" + logonName + "'}",
        contentType: "application/json; charset=utf-8;",
        dataType: "json",
        success: function (response) {
            if ($.parseJSON(response.d).runStatus) {
                location.reload();
            }
        },
        error: function (xhr, status, error) {
            var err = xhr.responseText;
            alert('Error: ' + err);
        }
    });
}

Open in new window


Sending to this method:
      [WebMethod]
      public static string BookTime(DateTime bookingDate, int durationHours, string logonName) {
         
      }

Open in new window


Gives me the error:

"{\"Message\":\"Invalid object passed in, \\u0027:\\u0027 or \\u0027}\\u0027 expected. (13): {bookingDate= \\u0027Mon Nov 30 06:30:00 UTC+0100 2015\\u0027, durationHours=0.5, logonName=\\u0027Admin\\u0027}\",\"StackTrace\":\"   at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeDictionary(Int32 depth)\\r\\n   at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth)\\r\\n   at System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String input, Int32 depthLimit, JavaScriptSerializer serializer)\\r\\n   at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit)\\r\\n   at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize[T](String input)\\r\\n   at System.Web.Script.Services.RestHandler.GetRawParamsFromPostRequest(HttpContext context, JavaScriptSerializer serializer)\\r\\n   at System.Web.Script.Services.RestHandler.GetRawParams
(WebServiceMethodData methodData, HttpContext context)\\r\\n   at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)\",\"ExceptionType\":\"System.ArgumentException\"}"

So first I wonder if I am sending over the date object correctly?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Manager / Solutions Architect
Commented:
The first main problem is that your data property is not passing valid JSON object.
You should pass it as:
data: {
  bookingDate: startTime, 
  durationHours: hours, 
  logonName: logonName
},

Open in new window

After this is just a matter of seeing if the date format is correct.
itniflProgrammer

Author

Commented:
Yes you were correct, the syntax was incorrect. I ended up with this, which works as long as durationHours is not a decimal number (the receiving method receives integers only),

data: "{bookingDate: "+JSON.stringify(startTime)+", durationHours:"+hours+", logonName:'" +logonName+"'}", 

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial