Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

223
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda