I am using the following function to send JSON formatted data to my Django based server which stores the user keys generated for users in a database. When I try to access the request.body on the server side using python on Django, it gets printed as a string [object Object]. How do I handle this part of parsing the json from the request.body on server side.
function completeKeygen(data)
{
var key_data = {
'user_id':user_id,
'keys':data
}
delete data['action'];
console.log("Sending UBE Keys to User");
console.log(key_data);
var oReq = new XMLHttpRequest();
oReq.open("POST",CLOUD_SERVER+'add_ube_keys',true);
oReq.responseType = "json";
oReq.setRequestHeader("Content-type", "application/json");
oReq.onload = function(oEvent){
console.log(oReq.response.success);
};
oReq.send(key_data);
}
For reference the server side code:
@csrf_exempt
def add_ube_keys(request):
data = request.POST
print data.get('user_id')
return_data = {
'success':True
}
rdata = json.dumps(return_data)
return HttpResponse(rdata, content_type='application/json')
Unlike the possibly duplicate question, I am getting the Querydict object on print request.POST returns <QueryDict: {}>. Why is it empty ?
print request.POSTreturns<QueryDict: {}>, not the one populated as given in the question stackoverflow.com/questions/24958705/django-request-post-json