I've been struggling with using JavaScript's fetch library to submit a form to my Django application. No matter what I try, it keeps throwing CSRF validation errors.
ajax
function myidLikeCom(params) {
$.ajax({
type: 'POST',
url: '{% url "boards:likeComment" %}',
data: {
postid: params,
csrfmiddlewaretoken: $('input[name=csrfmiddlewaretoken]').val(),
action: 'post'
},
success: function (json) {
document.querySelector(`#myidLikeCom${params}`).innerText = "json['result']";
},
error: function (xhr, errmsg, err) {
}
});
}
fetch doesn't seem to work for me
function myidLikeCom(params) {
let data = {
postid: params,
csrfmiddlewaretoken: $('input[name=csrfmiddlewaretoken]').val(),
}
fetch('{% url "boards:likeComment" %}', {
method: 'POST',
body: data,
})
}