admin管理员组文章数量:1026989
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.
- 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
4 Answers
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.
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.
- 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
4 Answers
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.
本文标签: javascriptjQuery omits Date values in params() and ajax()Stack Overflow
版权声明:本文标题:javascript - jQuery omits Date values in params() and ajax() - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745657894a2161708.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论