i using following function send json formatted data django based server stores user keys generated users in database. when try access request.body on server side using python on django, gets printed string [object object]. how handle part of parsing json 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 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 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 possibly duplicate question, getting querydict object on print request.post returns <querydict: {}>. why empty ?
i still don't know wrong approach in question mentioned above solved in 3 steps:
i added
json.stringify(key_data)in javascript before sending xmlhttprequest.changed
application/jsonapplication/x-www-form-urlencoded.now python side treats data normal string, used
json.loads(request.body)extract json object stringified data.
the above 2 steps have been taken posted question: how receive post data in python when using xmlhttprequest()
Comments
Post a Comment