在我的一個ASP.NET項目中,我用一些參數請求使用jQuery ajax請求的數據。其中之一是日期參數,格式爲 'DD/MM/YYYY'ASP.NET WebMethod 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。 –