How to pass json via a form element How to pass json via a form element json json

How to pass json via a form element


You must first "serialize" your JavaScript object into a JSON string using a library such as https://github.com/douglascrockford/JSON-js (there is no appropriate function built into JQuery.)


Of course django sees the string "Object object". That's excatly what jQuery writes into the input field. And what subsequently gets submitted. (Demo)

The swf_json parameter is an object to javascript. If you want to submit it as string I suggest slightly modifying the swf so that it passes a string to your function instead of an json object. Assuming you construct your json via string concatenation just add extra quotation marks at the end and beginning before calling submitForm

Instead of

{  "firstName": "John",  "lastName": "Smith"} //json object

try this

'{  "firstName": "John",  "lastName": "Smith"}' //string

If I remember correctly https://github.com/douglascrockford/JSON-js/blob/master/json2.js allows conversion between json and strings


Try jQuery function post

$.post("your/url/",        {"id":id},        function(data){},       "json");