It seems nobody has mentioned how to do this in pure JS using the X-CSRFToken
header and {{ csrf_token }}
, so here's a simple solution where you don't need to search through the cookies or the DOM:
var xhttp = new XMLHttpRequest();
xhttp.open("POST", url, true);
xhttp.setRequestHeader("X-CSRFToken", "{{ csrf_token }}");
xhttp.send();