I am aiming to transmit data from my Javascript file to the views file within my Django app using an Ajax request. However, I am opting to utilize solely Javascript since I lack familiarity with jQuery and am uncertain of how to incorporate the CSRF token.
Below is the snippet of my Javascript Code:
const request = new XMLHttpRequest();
request.open("POST", "/list");
var csrftoken = Cookies.get('csrftoken');
let data = {
items: JSON.stringify(items)
}
request.setRequestHeader( 'X-CSRF-TOKEN', csrftoken);
request.send(data);
I have experimented using Cookies.get('csrftoken')
as well as getCSRFTokenValue()
, yet I am uncertain about how to proceed once obtaining the token.
The Developers' Console displays:
Failed to load resource: the server responded with a status of 403 (Forbidden)