I’m writing a jQuery app that interfaces with a node.js server, so I’d like to pass JSON around everywhere.
It seems like jQuery’s $.post(url, data, ...) method sends its data in URL-encoded form. i.e, {foo: true, bar: "baz"} becomes foo=true&bar=baz, losing data type information.
Can I get jQuery to send the data in JSON format? Or should I just call $.post with a pre-JSON.stringify‘d string?
The short answer is that jQuery won’t do it for you. Pulled from the jQuery soure:
basically if the data isn’t a string coming in, jQuery is going to parse it into one just like you’re seeing. So if you want it to be JSON going to the server, you’re going to have to do it yourself. I’d suggest not using JSON.stringify since it’s not cross-browser. Here’s my “toJSON” function if you’re interested: https://gist.github.com/884348
You could always mess with $.ajax if you want. I’ve done it a fair bit.