我有两个Ajax请求。第一个从日期选择器获取日期,并检查数据库日期。一切正常。
第二个请求从日期选择器获取日期,并检索该月的第一个日期和该月的最后一个日期。这会导致错误。
这是WebMethod:
[System.Web.Services.WebMethod]
public static string GetDateFromDB(DateTime compareDate)
{
string selectedDate = compareDate.ToString("yyyy/MM/dd");
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["LoginDBConnectionString1"].ConnectionString);
// testing purpose
SqlCommand com = new SqlCommand("SELECT * from Holiday where Date='" + selectedDate + "'", conn);
SqlDataAdapter sqlDa = new SqlDataAdapter(com);
DataTable dt = new DataTable();
sqlDa.Fill(dt);
if (dt == null || dt.Rows.Count == 0)
return "NG";
else
return "OK";
}
[System.Web.Services.WebMethod]
public static string GetFirstDateAndLastDate(DateTime firstDayDate, DateTime lastDayDate, string ddlName)
{
string firstDay = firstDayDate.ToString("yyyy/MM/dd");
string lastDay = lastDayDate.ToString("yyyy/MM/dd");
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["LoginDBConnectionString1"].ConnectionString);
// testing purpose
SqlCommand com = new SqlCommand("SELECT sum([NumberOfHours]) FROM [LoginDB1].[dbo].[tbOT] where [Date] between Date = + '" + firstDay + "' And Date = + '" + lastDay + "' AND name = + '" + ddlName + "'", conn);
SqlDataAdapter sqlDa = new SqlDataAdapter(com);
DataTable dt = new DataTable();
sqlDa.Fill(dt);
return "NA";
}
脚本在下面:
jQuery(function($) {
$("[id$=datepicker]").datepicker({
onSelect: function(dateText) {
$(this).change();
$.ajax({
type: "POST",
url: "SupervisorOTRequest.aspx/GetDateFromDB",
data: '{ "compareDate" : "' + dateText + '"}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(response) {
if (response.d == "OK") {
alert("Today is Public Holiday!!!");
$("[id$=txtOTRate]").val(1);
} else if (response.d == "NG") {
var date1 = $("[id$=datepicker]").datepicker('getDate');
var day = date1.getDay();
if (day == 1 || day == 2 || day == 3 || day == 4 || day == 5 || day == 6) {
$("[id$=txtOTRate]").val(1.5);
} else {
$("[id$=txtOTRate]").val(2);
}
}
}
});
var selectedDate = $("[id$=datepicker]").datepicker('getDate');
// var selectedDate = gg.getDate();
//var selectedDate = $("[id$=datepicker]").val();
var firstDay = new Date(selectedDate.getFullYear(), selectedDate.getMonth(), 1);
var lastDay = new Date(selectedDate.getFullYear(), selectedDate.getMonth() + 1, 0);
var firstDayWithSlashes = (firstDay.getDate()) + '/' + (firstDay.getMonth() + 1) + '/' + firstDay.getFullYear();
var lastDayWithSlashes = (lastDay.getDate()) + '/' + (lastDay.getMonth() + 1) + '/' + lastDay.getFullYear();
var data = {};
data.fristDayDate = firstDayWithSlashes;
data.lastDayDate = lastDayWithSlashes;
data.ddlName = $("[id$=ddlName] option:selected").text();
$.ajax({
type: "POST",
url: "SupervisorOTRequest.aspx/GetFirstDateAndLastDate",
// data: '{ "fristDayDate" : "' + firstDayWithSlashes + '" + "lastDayDate" : "' + lastDayWithSlashes + '"}',
data: JSON.stringify(data),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(response) {
if (response.d == "NA") {
$("[id$=txtRemainHour]").val();
}
}
});
},
}).on("change", function() {});
function display(msg) {
$("<p>").html(msg).appendTo(document.body);
}
});
当检查“网络”>“标题”>“请求有效负载”下的页面时,页面正确显示(如下):
{fristDayDate:“ 1/6/2019”,lastDayDate:“ 30/6/2019”,ddlName:“ Masrudin Bin A B Rahman”}
ddlName:“ Masrudin Bin A B Rahman”
fristDayDate:“ 1/6/2019”
lastDayDate:“ 30/6/2019”
但是在预览下,错误消息如下所示:
{消息:“ 30/6/2019不是DateTime的有效值。”,…}
ExceptionType:“ System.FormatException”
消息:“ 30/6/2019不是DateTime的有效值。”
StackTrace:“
在System.ComponentModel.DateTimeConverter.ConvertFrom(ITypeDescriptorContext上下文,CultureInfo文化,对象值)
在System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeInternal(Object o,Type type,JavaScriptSerializer序列化器,布尔throwOnError,Object&convertedObject)
在响应下,错误消息如下所示:
{“ Message”:“ 30/6/2019不是DateTime的有效值。”,“ StackTrace”:“
在System.ComponentModel.DateTimeConverter.ConvertFrom(ITypeDescriptorContext上下文,CultureInfo文化,对象值)
在System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeInternal(Object o,Type type,JavaScriptSerializer序列化程序,布尔throwOnError,Object \ u0026 convertedObject)
在
答案 0 :(得分:0)
Karrieop,
尝试
DateTime firstDay= DateTime.ParseExact(firstDayDate, "yyyy/MM/dd", new CultureInfo("en-US"));