Estoy tratando de enviar POST con algunas variables a Django.
Enviar desde 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 ,}, }); }E intente capturar 'Song-Id' y 'Answer' en Django pero:
request.POST.get('Answer')devuelve Ninguno
¿Qué estoy haciendo mal?
ahora todo está bien. Cambié 'Tipo de contenido' y agregué 'nueva URLSearchParams' en el cuerpo
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, }), }); }Ahora puedo obtener mis variables en django
Pruebe request.body en lugar de request.POST