let lighter = false;
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 = jQuery.trim(cookies[i]);
// 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;
}
function editProfile() {
let span = document.getElementsByClassName('toInput');
if (!lighter) {
lighter = true;
for (let i = 0; i < span.length; i++) {
span[i].innerHTML = `<input style="margin-left: 5px" type="text" class="toInputInput" value="${span[i].innerText}" />`;
};
} else {
lighter = false;
let xhr = new XMLHttpRequest();
xhr.open('PATCH', '/orgEdit');
xhr.setRequestHeader("Accept", "application/json");
xhr.setRequestHeader("Content-Type", "application/json; charset=utf-8");
let spanInput = document.querySelectorAll('.toInputInput');
let csrftoken = getCookie('XSRF-TOKEN');
xhr.setRequestHeader("X-CSRFToken", csrftoken);
let bodyData = {
address: spanInput[0].value ?? '',
manager_phone: spanInput[1].value ?? '',
email: spanInput[2].value ?? '',
phone: spanInput[3].value ?? '',
buhgalter_phone: spanInput[4].value ?? '',
website: spanInput[5].value ?? '',
ogrn: spanInput[6].value ?? '',
inn: spanInput[7].value ?? '',
director: spanInput[8].value ?? '',
activity_type: spanInput[9].value ?? '',
'_token': csrftoken,
};
try {
console.log(bodyData)
xhr.send(bodyData);
xhr.onload = function () {
alert(`Загружено: ${xhr.status}`);
for (let i = 0; i < span.length; i++) {
span[i].innerHTML = `${span[i].getElementsByTagName('input')[0].value}`;
}
};
} catch (e) {
alert('запрос не удался')
}
};
}
"X-CSRFToken"
неправильный заголовок.let csrftoken = getCookie('XSRF-TOKEN');
получает значение.