我正在关注一个jQuery日历示例,如下所示,并尝试从我的SQL数据库加载日期而不是示例硬编码值,不确定什么是最好的方法,但我使用Ajax发布到我的Web方法并获取数据。来自数据库的数据将被加载到数据表中但问题是我不确定应该存储来自我的SQL数据的格式,这样当我检索它并将其返回到我的Ajax调用时,它可以替换“new Date( y,m,2)“。
请帮忙。感谢。
<script type='text/javascript'>
$(document).ready(function () {
var date = new Date();
var d = date.getDate();
var m = date.getMonth();
var y = date.getFullYear();
var sDate;
$.ajax({
type: "POST",
url: "/WebServices/Services.asmx/GetString",
contentType: "application/json; charset=utf-8",
async: false,
dataType: "json",
success: function (result) { sDate = result.d; }
});
alert(sDate);
$('#calendar').fullCalendar({
header: {
left: 'prev,next today',
center: 'title',
right: 'month,basicWeek,basicDay'
},
editable: true,
events: [
{
title: 'Click for Google',
start: new Date(y, m, 2), // Hardcoded date
url: 'http://google.com/'
}
]
});
});
</script>
[WebMethod]
public string GetString()
{ // Dump data from SQL database into DataTable
DataTable table = new DataTable();
table.Columns.Add("Date", typeof(DateTime));
table.Rows.Add(DateTime.Now);
return table;
}
答案 0 :(得分:0)
Date()构造函数可以使用许多不同的参数。毫秒非常稳定,但是如果你的数据库存储了Unix时间戳,记得在调用Date构造函数之前将它转换为毫秒(将它与1000相乘)。
旁注:您的代码无法按预期工作,因为在{ajax回调完成之前sDate
不可用。您需要执行以下操作:
$.ajax({
type: "POST",
url: "/WebServices/Services.asmx/GetString",
contentType: "application/json; charset=utf-8",
async: false,
dataType: "json",
success: onSuccess
});
function onSuccess(result) {
$('#calendar').fullCalendar({
header: {
left: 'prev,next today',
center: 'title',
right: 'month,basicWeek,basicDay'
},
editable: true,
events: [
{
title: 'Click for Google',
start: new Date( result.d*1000 ), // assuming UNIX time
url: 'http://google.com/'
}
]
});
}