2017-08-30 15 views
-1

在我的一個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)" 
+1

將其更改爲字符串,並將該方法內的字符串值轉換爲DateTime。 –

回答

0

首先發送到的WebMethod的參數和將WebMethod的參數不匹配:

param1 - p1
date - Date

如果您設置AJAX數據,如:

data: "{'param1': 'a', 'date1': '31/08/2017'}" 

更改從DateTimeString您的日期參數,那麼這個VB代碼應工作:

<WebMethod(EnableSession:=True)> 
Public Shared Function xyzMethod(ByVal param1 As String, ByVal date1 As String) 
    Dim convertedDate As DateTime = Convert.ToDateTime(date1) 
    'Return what you need 
    Return Nothing 
End Function 

注:如果我設定日期WebMethod的參數爲date,我得到以下錯誤:

Keyword is not valid as an identifier.

+0

查看更新的問題 –

+0

查看我的更新。您是否嘗試將WebMethod參數從「DateTime」更改爲「String」? – krlzlx