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

192
Views
Csrftoken is not defined

I'm using the following code as recommended by the documentation:

            function csrfSafeMethod(method) {
                // these HTTP methods do not require CSRF protection
                return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
            }
            $("#formTabla").submit(function(event){
                event.preventDefault();
                var formData = new FormData(this);
                $.ajax({
                    beforeSend: function(xhr, settings) {
                        if (!csrfSafeMethod(settings.type) && !this.crossDomain) {
                            xhr.setRequestHeader("X-CSRFToken", csrftoken);
                        }
                    }
                    url : "{% url 'submit' %}",
                    type: "POST",
                    processData: false,
                    contentType: false,
                    data: {
                        "checkboxes": formData,
                        "datos": todosDatos
                    },
                    success: function (respuesta){
                    }
                });
            });

I'm getting the error :

 "Uncaught ReferenceError: csrftoken is not defined".

I understand why this happens, but I have no idea how to solve it. How and where am I supposed to define crsftoken?

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

0

You can use ajaxSend() for this

$(document).ajaxSend(function(event, xhr, settings){
    if (!csrfSafeMethod(settings.type)) {
       xhr.setRequestHeader("X-CSRFToken", csrftoken);
    }
});
about 4 years ago · Juan Pablo Isaza Report

0

the variable is not defined anywhere, first you need to obtain csrf_token:

import Cookies from 'cookies-js'
let csrftoken  = Cookies.get('csrftoken')

then set the variable in request header.

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!