【问题标题】:How to pass a DateTime value to a WebMethod (ASMX)如何将 DateTime 值传递给 WebMethod (ASMX)
【发布时间】:2011-06-05 01:31:25
【问题描述】:

我有一个 WebMethod,其参数定义为 DateTime。当我这样称呼时

webservice,我得到这个错误:

在 System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializePrimitiveObject() 在 System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 深度)在 System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeDictionary(Int32 深度)在 System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 深度)在 System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(字符串 输入,Int32 深度限制, 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 序列化器)在 System.Web.Script.Services.RestHandler.GetRawParams(WebServiceMethodData 方法数据,HttpContext 上下文)在 System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext 上下文,WebServiceMethodData 方法数据)"

这是我的网络服务:

/// <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();
    }

}

这里是我的客户端代码:

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

                             }  
                         });

其中 'start' 和 'end' 时间是两个 javascript 'Date' 对象。

【问题讨论】:

    标签: asp.net json asmx webservice-client


    【解决方案1】:

    这对我有用:

    JSON.stringify(new Date())
    

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

    【讨论】:

      【解决方案2】:

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

      因此,您必须更改代码片段,例如

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

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

      以下插件的最快使用方法:

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

      您可以在以下文章中找到更多信息:

      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

      【讨论】:

      • jMsAjax 插件似乎不适用于最新版本的 jQuery (1.6.2)。
      • @Gan,您可以向作者报告您的问题/错误,作为对他博客条目的评论(答案中的链接)。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-06
      • 1970-01-01
      • 2021-05-15
      • 1970-01-01
      • 2011-06-14
      • 2010-12-02
      相关资源
      最近更新 更多