Search Here

Post Request Via Ajax in Django CSRF Token Missing or Incorrect

 In this article, you will learn how to make a POST request in Django using Ajax. To make an Ajax call you must include csrf token in your ajax post call request.

Post Request Via Ajax in Django CSRF Token Missing or Incorrect

CSRF Token Missing or Incorrect in Django

Below is the step-by-step guide on how to use Ajax with Django and make a POST request via ajax in Django.

Read More: Making Perfect SEO Friendly URL in Django Using Slug

First of all, I have an HTML file that contains a "textarea" input field and a button, the button has a JavaScript function called "ButtonSearchClick()". On that button click, we are going to make a POST request via ajax in Django.

Use Digital Marketing Tools Free

index.html

ajax_post.js

If you did not set the csrf token, and you make an ajax post request to Django then you got the following error.
Forbidden (CSRF token missing or incorrect.)

[Resolved] Forbidden (CSRF token missing or incorrect)

You can use one of the below codes before making an ajax POST request call to Django.

Solution 1

Solution 2

Post a Comment

1 Comments

  1. Great insights! I really appreciate how clearly you’ve outlined the topic. Your post has provided some valuable clarity. Thanks for sharing!

    ReplyDelete