jQuery.param() - 不序列化javascript Date对象? [英] jQuery.param() - doesn't serialize javascript Date objects?
问题描述
jQuery.param({foo: 1}); // => "foo=1" - SUCCESS!
jQuery.param({bar: new Date()}); // => "" - OUCH!
encodeURIComponent(new Date())没有问题,这就是我认为的param正在呼吁每个成员。
There is no problem with encodeURIComponent(new Date()), which is what I would have thought param is calling for each member.
另外,明确使用传统参数(例如jQuery.param(xxx,true))是否序列化日期,但是,唉,这不是由于我的数据结构不平坦,所以有很多帮助。
Also, explicitly using "traditional" param (e.g. jQuery.param(xxx, true)) DOES serialize the date, but alas, that isn't of much help since my data structure isn't flat.
这是因为typeof(Date)==object而param试图进入它以寻找它标量值?
Is this because typeof(Date) == "object" and param tries to descend into it to find scalar values?
如何实际序列化碰巧在其中包含Date的对象$ .post()等?
How might one realistically serialize an object that happens to have Date's in it for $.post() etc.?
推荐答案
您可能希望将日期转换为字符串,因为无论如何它都必须在电线上。
You're probably going to want the date transformed into a string, since that's what it's going to have to be on the wire anyway.
$.param({bar: new Date().toString()});
现在您可能希望以某种特定方式对其进行格式化,以便您的服务器获得可以解析的内容。我认为datejs库支持格式化,或者您可以通过使用 getDate()
, getMonth选择日期片段来自行推广( )
, getYear()
等。
Now you may want it formatted in some particular way so that your server gets something it can parse. I think that the datejs library has support for formatting, or you could roll your own by picking out pieces of the date with getDate()
, getMonth()
, getYear()
etc.
这篇关于jQuery.param() - 不序列化javascript Date对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!