Reputation: 6346
I'm returning some json from one of my controllers similar to the following:
render :json => {
:date => "new Date(new Date('Jan 01 2000').getTime() + #{500.seconds.to_i} * 1000)"
}
I'm expecting it to return a new date set to 500 seconds past midnight (or exactly 00:08:20). This json gets passed and processed by a JavaScript function that's expecting a date, however it seems to be interpreting it as a string.. I get an error message similar to:
Type mismatch. Value new Date(new Date('Jan 01 2000').getTime() + 500 * 1000) does not match type date
Running that in the Firebug console produces a perfectly valid date, however. How can I get JavaScript to inerpet my json as a date?
Edit
To provide a little more context I'm sending a series of JSON requests to a JavaScript function that build charts using Google's Visualization API.
Upvotes: 0
Views: 358
Reputation: 5920
JSON is not Javascript, it is just a data serialization format, much like XML.
What you have is actually a string of Javascript code that you need to interpret (or avoid having to interpret, preferably). The only way for that to be valid JSON is, indeed, for it to be a string value. Even then, it should not parse correctly without being inside an array or an object property.
You should refactor whatever's returning that Javascript code to instead return just 'Jan 01 2000'
, and then do that processing in Ruby. Or however it would be best refactored; it's hard to tell without more context.
Upvotes: 1