:(
I am making ajax requests, but I get this error:
Forbidden (CSRF token missing or incorrect.): /manager/ajax/
[23/Jun/2020 00:00:46] "POST /manager/ajax/ HTTP/1.1" 403 2517
[23/Jun/2020 00:01:18] "GET /manager/test/update/3/ HTTP/1.1" 200 10249
Forbidden (CSRF token missing or incorrect.): /manager/ajax/
[23/Jun/2020 00:01:18] "POST /manager/ajax/ HTTP/1.1" 403 2517
[23/Jun/2020 00:01:22] "GET /manager/test/update/3/ HTTP/1.1" 200 10249
Forbidden (CSRF token missing or incorrect.): /manager/ajax/
[23/Jun/2020 00:01:23] "POST /manager/ajax/ HTTP/1.1" 403 2517
JS:
function getCookie(name) {
var cookieValue = null;
if (document.cookie && document.cookie !== '') {
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
var cookie = cookies[i].trim();
// Does this cookie string begin with the name we want?
if (cookie.substring(0, name.length + 1) === (name + '=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break;
}
}
}
return cookieValue;
}
var csrftoken = getCookie('csrftoken');
fetch('/manager/ajax/', {
method: 'POST',
body: JSON.stringify({
csrfmiddlewaretoken: csrftoken,
title: 'hello!!!! :(',
}),
headers: {
"Content-Type": "application/x-www-form-urlencoded"
}
})
.then(response => response.json())
.then(json => console.log(json))
My view:
def delete_exam_question(request):
print(request.method)
if request.is_ajax:
print(request.POST)
#exam_question = ExamQuestion.objects.get(title = request.POST.get('title'))
#exam_question.delete()
return JsonResponse({'deleted': True})
I tried to fix it like in this question ("application/x-www-form-urlencoded"
), I already tried indicating "application/json"
, but it does not work... :(
I pass the token (csrftoken
), but it still doesn't work, I've tried everything, but I can't get it to work.
UPDATE:
("Content-Type"
=> "application/x-www-form-urlencoded"
)
Decorate the view, with the decorator csrf_exempt
. And the POST data are:
<QueryDict: {'{"csrfmiddlewaretoken":"4gTL9H17LIgTFc3HseYFuT6JQMbkEHxiuGXVxu9WWKTgN0gVmUtJJPDgwem0zj4U","title":"holaaa"}': ['']}>
That's why he gave me the error, but this, is very very rare. And I don't know how to correct it, any ideas? I don't know much JavaScript.
I changed the Conten-Type
, to "application/json"
, and the POST data are:
<QueryDict: {}>
but, i print request.body
, and printed that:
b'{"csrfmiddlewaretoken":"4gTL9H17LIgTFc3HseYFuT6JQMbkEHxiuGXVxu9WWKTgN0gVmUtJJPDgwem0zj4U","title":"holaaa"}'
what's going on? :(