简体   繁体   中英

I wanted to do a post request with django using ajax

I want to make a request with the method post using ajax . But my django server returns a Forbidden error (CSRF token missing or incorrect.): / Post / it asks me for the csrf token

here is my code:

{% block java %}
<script type="text/javascript">

var xhr = new XMLHttpRequest()


document.getElementById('form').addEventListener("submit", function(event) {
         event.preventDefault();

        xhr.onload = function(){
                console.log("succes")
                
        }
xhr.open("POST", {% url 'post' %})
xhr.send()


});
</script>
{% endblock %}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM