Answer the question
In order to leave comments, you need to log in
How to pass csrf token to ajax django?
Hello.
Added to the form:
{% csrf_token %}
Made a script:
<script>
$(".arsenal_vote_answer").click(function(e) {
e.preventDefault();
$.ajax({
type: "POST",
url: '/arsenal_votedone{{ arsenalitem.id }}',
data:{
arsenal: $('.arsenal_label').val(),
csrfmiddlewaretoke: $('input[name=csrfmiddlewaretoke]').val(),
},
success: function(data){
if (data) {
$('.arsenal_vote_figure').html(data.ars1)
$('.arsenal_vote_figure1').html(data.ars2)
$('.arsenal_vote_figure2').html(data.ars3)
}
else
{}
}
});
});
</script>
Answer the question
In order to leave comments, you need to log in
Try to take the token not from the form input, but from the cookie
var a = document.cookie.split(';');
var token = ''
for (i = 0; i < a.length; i++) {
var b = a[i].split('=')
b[0] = b[0].replace(/\s+/g, '')
if (b[0] == 'csrftoken') {
token = b[1]
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question