Monday, May 13, 2024
 Popular · Latest · Hot · Upcoming
180
rated 0 times [  187] [ 7]  / answers: 1 / hits: 12099  / 10 Years ago, tue, march 25, 2014, 12:00:00

I'm newbie to django. I'm creating a simple app that posts user data to Django server. But I'm facing a problem regarding queryDict. It's empty. The code is:



@csrf_exempt
def create_user(request):

This function creates users
:param request: post request from front-end
:return: success/failure

if request.method == 'POST':
#x = json.loads(request.POST)
print(request.POST)
return JSONResponse(request.POST)


The POST request is :



function post(){
xmlhttp = new XMLHttpRequest();
var url = http://127.0.0.1:8000/create_user/;
xmlhttp.open(POST, url, true);
//xmlhttp.setRequestHeader(Content-type, application/json);
xmlhttp.onreadystatechange = function () { //Call a function when the state changes.
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
alert(xmlhttp.responseText);
}
}
var parameters = {
username: myname,
password: mypass
};
xmlhttp.send(JSON.stringify(parameters));
}

More From » python

 Answers
25

You're sending data in the body of the request:



xmlhttp.send(JSON.stringify(parameters));


This data will then end up in the request.body.



This is not the same as POSTing a HTML form, so it won't end up in the request.POST. If you want to simulate a HTML form POST, see this answer.


[#46577] Monday, March 24, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
lucianom

Total Points: 601
Total Questions: 98
Total Answers: 109

Location: Kenya
Member since Fri, Dec 23, 2022
1 Year ago
lucianom questions
Tue, Feb 22, 22, 00:00, 2 Years ago
Wed, May 5, 21, 00:00, 3 Years ago
Sun, Jan 24, 21, 00:00, 3 Years ago
Sat, Aug 15, 20, 00:00, 4 Years ago
Mon, Jun 22, 20, 00:00, 4 Years ago
;