Fix CSRF token missing error in department creation modal

This commit is contained in:
admin i 2025-11-27 15:08:00 +03:00
parent 2e2ba72766
commit 783ea4f31f

View File

@ -131,6 +131,23 @@ export function initCreateDepartmentModal(options = {}) {
return meta ? meta.content : '';
}
/**
* Получить CSRF токен из формы или cookie
*/
function getCSRFToken() {
// Сначала пробуем получить из формы
const tokenInput = form.querySelector('[name="csrfmiddlewaretoken"]');
if (tokenInput && tokenInput.value) {
return tokenInput.value;
}
// Если нет в форме, пробуем получить из cookie
const cookieValue = document.cookie
.split('; ')
.find(row => row.startsWith('csrftoken='));
return cookieValue ? cookieValue.split('=')[1] : '';
}
// Очистка формы при открытии модала
modal.addEventListener('show.bs.modal', function() {
form.reset();
@ -163,6 +180,7 @@ export function initCreateDepartmentModal(options = {}) {
};
const token = getJWTToken();
const csrfToken = getCSRFToken();
const headers = {
'Content-Type': 'application/json',
'X-Requested-With': 'XMLHttpRequest',
@ -172,6 +190,10 @@ export function initCreateDepartmentModal(options = {}) {
headers['Authorization'] = `Bearer ${token}`;
}
if (csrfToken) {
headers['X-CSRFToken'] = csrfToken;
}
try {
const response = await fetch(submitUrl, {
method: 'POST',