Relative Content

Tag Archive for javascriptpythondjangoformsfetch-api

How to send authentication session id to django backend using fetch api?

saveBtn.addEventListener(‘click’, function() { const csrftoken = getCookie(‘csrftoken’); fetch(‘http://localhost:8000/card/{{ auction.slug }}/add/’, { method: ‘GET’, credentials: ‘include’, headers:{ ‘Content-Type’:’application/json’, ‘X-Requested-With’: ‘XMLHttpRequest’, ‘X-CSRFToken’: csrftoken, }, }) .then(function(response) { if (!response.ok) { throw new Error(‘Network response was not ok’); } return response.json(); }) .then(function(data) { if (data.added === true) { saveBtn.innerHTML = ‘Saved’; } else { saveBtn.innerHTML = ‘ […]