Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

68
Views
Send POST from JS to Django

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?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

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

about 4 years ago · Juan Pablo Isaza Report

0

Try request.body instead of request.POST

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!