javascript - jQuery omits Date values in params() and ajax() - Stack Overflow

I have a simple Javascript object like this:var data = { date: new Date(), plainText: "test"

I have a simple Javascript object like this:

var data = { date: new Date(), plainText: "test" };

when I user $.params(data) to build a query string I get this:

plainText=test

Meanwhile the date value is omitted.

Likewise when I use $.ajax() the date value is missing as well.

Is there a way to get jQuery to include the date value as a parameter?

date.toString() or date.toJSON() would both be fine with me.

I have a simple Javascript object like this:

var data = { date: new Date(), plainText: "test" };

when I user $.params(data) to build a query string I get this:

plainText=test

Meanwhile the date value is omitted.

Likewise when I use $.ajax() the date value is missing as well.

Is there a way to get jQuery to include the date value as a parameter?

date.toString() or date.toJSON() would both be fine with me.

Share Improve this question asked Feb 14, 2012 at 15:04 TimmTimm 2,6623 gold badges26 silver badges35 bronze badges 3
  • Can't you have the date.toJSON() in your data object instead? – Linus Thiel Commented Feb 14, 2012 at 15:07
  • @LinusGThiel yes, that's exactly what i think – Nicola Peluchetti Commented Feb 14, 2012 at 15:18
  • Since this issue occurs with a variety of different objects I was looking for a generic solution. But I agree, that it would be cleaner to convert the date to string somewhere else, and not rely on legacy behavior in jQuery. – Timm Commented Feb 14, 2012 at 15:40
Add a ment  | 

4 Answers 4

Reset to default 1

$.params(data, true) will convert the date .toString() and it will appear in the result, but do you really want a textual represenation of the date? There's no one standard for converting dates into query strings, just choose the format you want and convert your date into it before sending it to the server...

Or convert to JSON.

Use JSON.stringify(new Date()).

var data = { date: JSON.stringify(new Date()), plainText: "test" };

Note: This will get the time part from the date also.

JSON library is natively supported in all the browsers but for browsers which do not support it you can include this js file http://ajax.cdnjs./ajax/libs/json2/20110223/json2.js

You can also use (new Date()).toJSON().

var data = { date: (new Date()).toJSON(), plainText: "test" };

If you just want the date part to be sent then you can use this.

var date = new Date();
//change the format as per your need, this is in mm/dd/yyyy format
date = (date.getMonth() + 1) + "/" + date.getDate() + "/" + date.getFullYear();

var data = { date: date, plainText: "test" };

You should do

 var data = { date: (new Date()).toJSON(), plainText: "test" };

This results in something like

{ date="2012-02-14T15:08:04.110Z", plainText="test"}

Look at the fiddle http://jsfiddle/Vj3n7/

A generic reusable jQuery plugin

This plugin of mine makes it easy to pre-prepare plex JSON objects for server posting. It takes care of dates as well. And it works no matter whether browser natively support JSON functions or not. And an extension to convert back to dates when receiving data from server.

Posting to server

Link to my blog post with detailed information an code you can freely use. This plugin can be used for posting data to server.

Receiving from server

There's another jQuery this time extension of mine that also takes care of automatic date conversion when data gets back from the server to client. I've extended parseJSON function to take care of date conversion (conversion is optional, but code can be changed to always take care of dates). It can convert ISO dates as well as Asp encoded dates to Javascript Date instances.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745657896a4638654.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信