【发布时间】:2017-08-30 10:22:13
【问题描述】:
在我的一个 ASP.NET 项目中,我使用带有一些参数的 jQuery ajax 请求来请求数据。其中之一是日期参数,格式为 'dd/MM/yyyy'
$.ajax({
type: "POST",
url: "somePath/xyzMethod",
data: "{'param1': 'a', 'date': '31/08/2017'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
async:true,// false,
success: functions(response) { }
});
在我的 .vb 文件中,我有以下用于上述 ajax 调用的 WebMethod:
<WebMethod(EnableSession:=True)>
Public Shared Function xyzMethod(ByVal param1 As String, Byval date as
DateTime)
//End Function
当进行 ajax 调用时,我收到以下错误:
{Message: "30/08/2017 is not a valid value for DateTime.",…}
ExceptionType
:
"System.FormatException"
Message
:
"30/08/2017 is not a valid value for DateTime."
StackTrace
:
" at System.ComponentModel.DateTimeConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)
↵ at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeInternal(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject)
↵ at System.Web.Script.Services.WebServiceMethodData.StrongTypeParameters(IDictionary`2 rawParams)
↵ at System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext context, WebServiceMethodData methodData, IDictionary`2 rawParams)
↵ at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)"
【问题讨论】:
-
将其更改为字符串并将方法中的字符串值转换为 DateTime。
标签: jquery asp.net vb.net date