Ticket #197 (closed feature: invalid)
$.postJSON
| Reported by: | cdomigan | Owned by: | |
|---|---|---|---|
| Priority: | minor | Milestone: | |
| Component: | ajax | Version: | |
| Keywords: | json | Cc: | |
| Blocking: | Blocked by: |
Description
A $.postJSON method would be handy.
Change History
comment:3 Changed 7 years ago by bryan@…
- Keywords json added
I've used this script quite successfully: http://www.thomasfrank.se/json_stringify_revisited.html
I have an app that stores data in a JSON object as a user is doing things. In order to get that JS object to the server to do something useful with it, it has to be converted to a string first. "JSONstring.make(myobject)" does just that. It's like Serialize, but for JSON.
I agree it would be useful to have build it, but I don't mind including an external script to do it.
comment:4 Changed 4 years ago by cjwilks
The problem with the workaround suggested by joern is that it doesn't actually enable you to POST (read: send) JSON. It simply allows you to send form data using post, and then receive JSON in return. A true postJSON function would let you send JSON. It would accept a javascript object in the "data" input and automatically:
# serialize it to a string # set the content type to application/json (as specified in RFC 4627)
To actually support this, jquery would need to include a small JSON stringifier.
Please follow the bug reporting guidlines and use jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

If you need this alot, you could just add it as a plugin:
$.postJSON = fuction(url, data, callback) { $.post(url, data, callback, "json"); };This saves about 2 characters per use, not really that very handy...