How to pass a DateTime value to a WebMethod (ASMX)
我有一个webmethod,它的参数定义为datetime。当我叫它的时候
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)"
这是我的网络服务:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | /// <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(); } } |
在这里,我的客户端代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | 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 AJAX所期望的特定日期/时间线格式-其形式为
因此,您必须更改代码片段,例如
1 | `'", start:/Date('+ start.getTime()+')/, end...` |
到
1 | '", 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
这对我很有用:
1 | JSON.stringify(new Date()) |
这会将其转换为类似"2014-06-04t14:26:27.129z"的格式,我的Web服务很乐意接受该格式作为日期时间参数。