Key and value reversed in JSON string sent to Django view via POST
This is my first attempt at ajax, and I've written a submit handler that parses a form and sends the data via POST to the server as a JSON string. Here is a simplified example of what my javascript looks like开发者_运维知识库
formData = JSON.stringify({'testA':{'testa':'some data'},'testB':{'test2':'more data'}});
The JSON string looks like this
{"testA":{"test1":"some data"},"testB":{"test2":"more data"}}
and I send it via post here
$.post("/some/form/page/",formData,updateForm,'json');
On the server side is where the problem rears its ugly head, this is what my query dictionary looks like when I print if from the Django view
<QueryDict: {u'{"testA":{"test1":"some data"},"testB":{"test2":"more data"}}': [u'']}>
The JSON string is the key of the query dictionary. I am not very familiar with Javascript or JSON so don't be afraid of hurting my pride by pointing out an obvious newbie mistake, because I am and I know it. ;)
Thanks,
You're sending the string as a parameter to $.post
. Instead of calling "JSON.stringify()" yourself, just pass in your raw JavaScript object as the second parameter to $.post()
.
$.post("/some/form/page/", {'testA':{'testa':'some data'},'testB':{'test2':'more data'}}, updateForm, 'json');
精彩评论