JQuery omits date values ​​in params () and ajax ()

I have a simple Javascript object like this:

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

when I am a user $.params(data)to build a query string, I get the following:

plainText=test

Meanwhile, the meaning is dateomitted.

Similarly, when I use $.ajax(), the value is datealso missing.

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

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

+5
source share
4 answers

$.params(data, true) .toString(), , ? , , ...

JSON.

+1

JSON.stringify(new Date()).

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

. .

JSON , , , js http://ajax.cdnjs.com/ajax/libs/json2/20110223/json2.js p >

(new Date()).toJSON().

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

, , .

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" };
+1

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

-

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

http://jsfiddle.net/Vj3n7/

+1

jQuery

JSON . . , JSON . .

- , . .

There is another jQuery extension this time , which also provides automatic date conversion when data is returned from the server to the client. I expanded the function parseJSONto take care of the date conversion (conversion is optional, but the code can be changed to always take care of the dates). It can convert ISO dates, as well as dates encoded in Asp.net, to Javascript instances Date.

+1
source

All Articles