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

232
Vistas
How to add CSRF_Token in Ajax call when not using a form?

So in the past I've used forms to send information and have managed to include a csrf token into the form but this time I'm sending the information from a javascript page without a form. Im getting an error : "POST http://127.0.0.1:8000/test 403 (Forbidden)" which is probably due to the fact that I dont have a csrf token included. However I'm not sure where and how to implement that in this case. Thanks for your time.

In my item-store.html I have so irrelevant code and at the bottom I have :

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    <script type="text/javascript" src="staticfiles/item-store.js"></script>

And then here is my item-store.js file :

var item_mode = "all";
console.log("Connected...")
console.log(item_mode)

// Main Get Element for All items
document.getElementById("All_Items").onclick = function() {All_Items_func()};

// Sub Get Elements for Specific items
document.getElementById("Meat_Seafood").onclick = function() {Meat_Seafood_func()};
document.getElementById("Bakery").onclick = function() {Bakery_func()};
document.getElementById("Fruits").onclick = function() {Fruits_func()};
document.getElementById("Vegetables").onclick = function() {Vegetables_func()};
document.getElementById("Dairy").onclick = function() {Dairy_func()};

function All_Items_func() {
  item_mode = "all";
  console.log(item_mode);
  Send_Python_Info();
}

function Meat_Seafood_func() {
  item_mode = "meat&seafood";
  console.log(item_mode);
  Send_Python_Info();
}

function Bakery_func() {
  item_mode = "bakery";
  console.log(item_mode);
  Send_Python_Info();
}

function Fruits_func() {
  item_mode = "fruits";
  console.log(item_mode);
  Send_Python_Info();
}

function Vegetables_func() {
  item_mode = "vegetables";
  console.log(item_mode);
  Send_Python_Info();
}

function Dairy_func() {
  item_mode = "dairy";
  console.log(item_mode);
  Send_Python_Info();
}

// Send info to Python File
function Send_Python_Info() {
  ToSend = JSON.stringify(item_mode)
  console.log(ToSend)
  window.alert(ToSend)
  $.ajax({
    url:"/test",
    type:"POST",
    contentType: "application/json",
    data: JSON.stringify(ToSend),
    success: function () {
      alert("Saved!");
    }
  });
}
about 4 years ago · Juan Pablo Isaza
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