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

222
Views
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
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!