There is a client-side JavaScript and server-side Python, powered by Django. There is a data object: foo_data = {"foo":1, "bar":2}.
Now, I would like to send a post-request using dojo/request/xhr, and send foo_data along with another variable:
xhr.post(document.location.href, {
data: {
'is_foo': true,
'data': foo_data
},
headers: { 'Content-Type': 'application/json' }
}).then(function(text){
console.log('The server returned: ', text);
});
And then read sent data in Django's views.py file:
def post(self, request, *args, **kwargs):
json.loads(request.body)
BUT, it doesn't work:
- if I ssend
foo_data, python doesn't recognize it correctly as JSON object and can't read it usingjson.loads. - I can't encode
foo_datausingJSON.parsebecause it is already an object! request.POSTis an emptyQueryDictrequest.bodyhas string wordobject(instead of the real object)
Any ideas how to solve this?
Goal: send JSON object from JS --> Python and read it on server-side.