如何将DateTime值传递给WebMethod(ASMX)

时间:2022-12-03 22:55:57

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

我有一个WebMethod,其参数定义为DateTime。我打电话的时候

webservice, I get this error:

webservice,我收到此错误:

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

在System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializePrimitiveObject()在System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(深度Int)在System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeDictionary(深度Int)在的System.Web .Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(深度Int)在System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(字符串输入,的Int32 depthLimit,JavaScriptSerializer串行器)在System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer串器,串输入,类型类型,的Int32 depthLimit)在System.Web.Script.Serialization.JavaScriptSerializer.Deserialize [T](字符串输入)在System.Web.Script.Services.RestHandler.GetRawParamsFromPostRequest(HttpContext的上下文中,JavaScriptSerializer串行器)在系统。 Sys的Web.Script.Services.RestHandler.GetRawParams(WebServiceMethodData methodData,HttpContext context) tem.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context,WebServiceMethodData methodData)“

This is my WebService:

这是我的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.

'开始'和'结束'时间是两个javascript'日期'对象。

2 个解决方案

#1


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

这是因为ASP.NET Ajax期望有特定的日期/时间有线格式 - 它的形式为“\ / Date(x)\ /”,其中x是自1970年1月1日UTC午夜以来经过的ms数。所以基本上,你需要使用一些辅助函数,在调用服务时将JS日期转换为所需的格式(反之亦然,日期/时间json从服务到JS日期/时间对象)。

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/

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://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://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

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

#2


1  

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.

这会将其转换为“2014-06-04T14:26:27.129Z”之类的格式,我的Web服务很乐意接受DateTime参数。

#1


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

这是因为ASP.NET Ajax期望有特定的日期/时间有线格式 - 它的形式为“\ / Date(x)\ /”,其中x是自1970年1月1日UTC午夜以来经过的ms数。所以基本上,你需要使用一些辅助函数,在调用服务时将JS日期转换为所需的格式(反之亦然,日期/时间json从服务到JS日期/时间对象)。

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/

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://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://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

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

#2


1  

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.

这会将其转换为“2014-06-04T14:26:27.129Z”之类的格式,我的Web服务很乐意接受DateTime参数。