最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jQuery.param() - doesn't serialize javascript Date objects? - Stack Overflow

programmeradmin2浏览0评论
jQuery.param({foo: 1});             // => "foo=1" - SUCCESS!
jQuery.param({bar: new Date()});    // => "" - OUCH!

There is no problem with encodeURIComponent(new Date()), which is what I would have thought param is calling for each member.

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.

Is this because typeof(Date) == "object" and param tries to descend into it to find scalar values?

How might one realistically serialize an object that happens to have Date's in it for $.post() etc.?

jQuery.param({foo: 1});             // => "foo=1" - SUCCESS!
jQuery.param({bar: new Date()});    // => "" - OUCH!

There is no problem with encodeURIComponent(new Date()), which is what I would have thought param is calling for each member.

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.

Is this because typeof(Date) == "object" and param tries to descend into it to find scalar values?

How might one realistically serialize an object that happens to have Date's in it for $.post() etc.?

Share Improve this question asked May 8, 2010 at 15:34 user336234user336234 512 bronze badges 3
  • What version of jQuery are you using? – James Commented May 8, 2010 at 16:11
  • 1 That's not what happens for me... :\ jQuery.param({bar: new Date()}); = bar=Sat+May+08+2010+17%3A17%3A42+GMT%2B0100+(GMT+Daylight+Time). – Matt Commented May 8, 2010 at 16:18
  • @Matt: I'm using 1.4.2. Do you by chance have traditional=true somehow defaulted? That's the only way I get your result. Any thoughts on why we're seeing different behaviors? I like yours better :-) – user336234 Commented May 8, 2010 at 17:34
Add a ment  | 

3 Answers 3

Reset to default 6

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()});

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.

If you work with Microsoft products on the server side you should take in consideration, that Microsoft serialize Date as a number of milliseconds since UTC, so as a number. To be more exact, the serialization string look like /Date(utcDate)/, where utcDate date is this number. Because JSON supports the backslash as an escape character you should use code like following to serialize a Date object myDate:

"\/Date(" +  Date.UTC(myDate.getUTCFullYear(), myDate.getUTCMonth(),
                      myDate.getUTCDate(), myDate.getUTCHours(),
                      myDate.getUTCMinutes(), myDate.getUTCSeconds(),
                      myDate.getUTCMilliseconds()) + ")\/"

I think this is a jQuery bug in the following context:

  • jQuery 1.4.2 (1.3.2 works)
  • new methods added into Date.prototype
发布评论

评论列表(0)

  1. 暂无评论