Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

193
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda