Reputation: 6107
I have a javascript object that looks like this:
var myObject = { "Danny": {"height": 1.70, "weight" : 70 }, "David" : {"height": 1.90, "weight" : 80" } ... }
I want to send it as a JSON to a django view and parse it in it. On the client side, using jQuery, I added this:
$.ajax({
type: "POST",
url: "/ajax/someview",
data: JSON.stringify(myObject),
contentType: 'application/json; charset=utf-8',
dataType: "json"
});
However, when I debug the view and look at request.POST, the data looks like this:
POST:<QueryDict: {u'{"Danny": {"height": 1.70, "weight" : 70 }, "David" : {"height": 1.90, "weight" : 80" }}' : [u'']}>
How can I convert this to a python object which i can work it (using simplejson.load)?
Thanks, Joel
Upvotes: 1
Views: 1555
Reputation: 599610
You can access the raw POST data with - not surprisingly - request.raw_post_data
. That will give you a string you can convert with simplejson.loads()
.
Upvotes: 4
Reputation: 31951
$.ajax({
type: "POST",
url: "/ajax/someview",
data: {'mydata': JSON.stringify(myObject)},
contentType: 'application/json; charset=utf-8',
dataType: "json"
});
Then get it with request.POST.get('mydata')
Upvotes: 1