I'm trying to send POST with some variables to Django.
Send from JS:
function sendStatistic(csrftoken, song_id, answer) {
fetch('request/', {
method: 'POST',
headers: {
'X-CSRFToken': csrftoken,
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: {
'Song-Id': song_id,
'Answer': answer
,},
});
}
And try to capture 'Song-Id' and 'Answer' in Django but:
request.POST.get('Answer')
returns None
what am I doing wrong?
CodePudding user response:
now all is ok. I have changed 'Content-Type' and add 'new URLSearchParams' into body
function sendStatistic(csrftoken, song_id, answer) {
fetch('request/', {
method: 'POST',
headers: {
'X-CSRFToken': csrftoken,
'Content-Type': 'application/x-www-form-urlencoded',
},
body: new URLSearchParams({
'Song-Id': song_id,
'Answer': answer,
}),
});
}
Now I can get my variables in django
CodePudding user response:
Try request.body instead of request.POST