ko.toJSON() 可以处理日期吗?
我在 ASP.NET MVC 页面上使用 KnockoutJS。我使用 ajax 通过调用 ko.toJSON(viewModel) 来将表单保留回服务器,然后使用 jQuery 将结果发布回服务器。视图模型上的所有属性均已成功序列化,但 Javascript 日期除外,该日期保留为空对象。
声明:
var viewModel = {
startTime: ko.observable(),
type: ko.observable(),
durationInMinutes: ko.observable(),
notes: ko.observable()
};
保存数据:
var postData = ko.toJSON(viewModel);
$.ajax({
url: "/data",
type: "POST",
data: postData,
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function () {
console.log('success!');
},
error: function () {
console.log('fail!');
}
});
viewModel.startTime() 的 console.log 值为:
日期 {2011 年 5 月 10 日星期二 11:30:00 GMT-0500(中部夏令时间)}
在保存数据第 1 行之后,postData 的值为:
{
"startTime": {},
"type": "1",
"durationInMinutes": "45",
"notes": "asfasdfasdfasdfasdfasdfasdfas",
"displayableStartTime": "10-May 11:30"
}
如果我展开保存数据的第 1 行到
var jsonEvent = ko.toJS(viewModel);
jsonEvent.startTime = viewModel.startTime();
var postData = JSON.stringify(jsonEvent);
postData 的值是:
{
"startTime": "2011-05-10T16:30:00.000Z",
"type": "1",
"durationInMinutes": "45",
"notes": "asfasdfasdfasdfasdfasdfasdfas",
"displayableStartTime": "10-May 11:30"
}
任何人都可以解释可能发生的情况以及我如何能够让 knockoutjs 处理日期对象?
I am using knockoutjs on an asp.net mvc page. I am using ajax to persist a form back to the server by calling ko.toJSON(viewModel)
and then posting the results back to the server using jQuery. All of the properties on the view model are successfully serialized except for the Javascript date which is persisted as an empty object.
Declaration:
var viewModel = {
startTime: ko.observable(),
type: ko.observable(),
durationInMinutes: ko.observable(),
notes: ko.observable()
};
Save Data:
var postData = ko.toJSON(viewModel);
$.ajax({
url: "/data",
type: "POST",
data: postData,
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function () {
console.log('success!');
},
error: function () {
console.log('fail!');
}
});
The console.log value of viewModel.startTime() is:
Date {Tue May 10 2011 11:30:00 GMT-0500 (Central Daylight Time)}
After line 1 of Save Data, the value of postData is:
{
"startTime": {},
"type": "1",
"durationInMinutes": "45",
"notes": "asfasdfasdfasdfasdfasdfasdfas",
"displayableStartTime": "10-May 11:30"
}
If I expand line 1 of Save Data to
var jsonEvent = ko.toJS(viewModel);
jsonEvent.startTime = viewModel.startTime();
var postData = JSON.stringify(jsonEvent);
The value of postData is:
{
"startTime": "2011-05-10T16:30:00.000Z",
"type": "1",
"durationInMinutes": "45",
"notes": "asfasdfasdfasdfasdfasdfasdfas",
"displayableStartTime": "10-May 11:30"
}
Can anyone explain what might be going on and how I might be able to get knockoutjs to handle the date object?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
鉴于 ko.toJS 和日期的当前问题,一种选择是创建一个 dependentObservable ,其中包含您希望服务器处理的实际值。
类似于:
现在,当您调用 ko.toJSON 时,您将获得带有服务器可以使用的正确值的 startTime 。
对于较旧的浏览器,诸如 json2.js 之类的内容将包含 Date 对象的 .toJSON。
Given the current issue with ko.toJS and dates, one option would be to create a dependentObservable containing the real value that you want the server to deal with.
Something like:
Now, when you call
ko.toJSON
you will get thestartTime
with the correct value that the server could use.For older browsers, something like json2.js would include the .toJSON for Date objects.
当日期为
DateTime.MinValue
时,我遇到了ko.toJSON()
给我提供错误日期格式的问题。虽然可能无法解决您的问题,但此修复适用于我的 ko.toJSON() 日期问题:
ASP.Net WebMethod 失败,因为 ko.toJSON() 为 DateTime.MinValue 生成不同的结果
I had a problem with
ko.toJSON()
giving me a bad date format when the date wasDateTime.MinValue
.Though probably not a fix for your problem, this fix worked for my
ko.toJSON()
date problem:ASP.Net WebMethod fails because ko.toJSON() produces different results for DateTime.MinValue