How to pass a DateTime value to a WebMethod (ASMX)
Asked Answered
M

2

11

I have a WebMethod with a parameter defined as DateTime. When I call that

webservice, I get this error:

at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializePrimitiveObject() at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth) at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeDictionary(Int32 depth) at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth) at System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String input, Int32 depthLimit, JavaScriptSerializer serializer) at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit) at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize[T](String input) at System.Web.Script.Services.RestHandler.GetRawParamsFromPostRequest(HttpContext context, JavaScriptSerializer serializer) at System.Web.Script.Services.RestHandler.GetRawParams(WebServiceMethodData methodData, HttpContext context) at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)"

This is my WebService:

/// <summary>
/// Summary description for AgendamentoService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class AgendamentoService : System.Web.Services.WebService
{

    public AgendamentoService()
    {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
    public CompromissoWekCalendarVO[] GetCompromissos(int id_pessoa, DateTime start, DateTime end)
    {
        bo.CompromissoBO compBO = new bo.CompromissoBO();
        return compBO.Get(id_pessoa,start, end).ToArray();
    }

}

And here, my client side code:

 var params =  '{id_pessoa: "' + id_pessoa + '", start:/Date('+ start.getTime()+')/, end:/Date(' + end.getTime()+')/}';
                    $.ajax(  
                         {  
                             type: "POST",  
                             dataType: "json",
                             contentType: "application/json; charset=utf-8",  
                             url: '<%= this.ResolveClientUrl("~/services/misc/AgendamentoService.asmx/GetCompromissos") %>',  
                             data: params,  
                             success: function (json) {  

                                if ($.isArray(json.d)) {
                                  $.each(json.d, function(key, value) {
                                    value.start = getJsonDate(value.start);
                                    value.end = getJsonDate(value.end);
                                  });
                                }

                                callback(json.d);                                 

                             }  
                         });

Where 'start' and 'end' time are two javascript 'Date' objects.

Minnesota answered 7/1, 2011 at 3:6 Comment(0)
L
9

Thats because there is specific date/time wire format that ASP.NET Ajax expects - its of form of "\/Date(x)\/", where x is the number of ms elapsed since Jan. 1, 1970 at midnight UTC. So essentially, you need to use some helper function that will convert your JS dates into the needed format while calling the service (and vice versa, date/time json from service to JS date/time object).

So, you have to change code fragment such as

`'", start:/Date('+ start.getTime()+')/, end...` 

to

'", start:"\\\/Date(' + this.getTime() + ')\\\/", end...'

Quickest way to use below plug-in:

http://schotime.net/blog/index.php/2008/07/01/jquery-plugin-for-aspnet-ajax-jmsajax/

You can find more info in below articles:

http://www.overset.com/2008/07/18/simple-jquery-json-aspnet-webservice-datetime-support/

http://schotime.net/blog/index.php/2008/06/19/jquery-ajax-aspnet-and-dates/

http://msmvps.com/blogs/luisabreu/archive/2009/08/19/jquery-full-control-with-the-ajax-function.aspx

Lectionary answered 7/1, 2011 at 5:2 Comment(2)
The jMsAjax plugin seems do not work with latest version of jQuery (1.6.2).Teary
@Gan, you may report your problem/bug to the author as comment to his blog entry (the link in the answer).Lectionary
W
3

This worked for me:

JSON.stringify(new Date())

This converts it into a format like "2014-06-04T14:26:27.129Z", which my web service is happy to accept for a DateTime parameter.

Walliw answered 4/6, 2014 at 14:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.