Fix CSRF token missing error in department creation modal
This commit is contained in:
parent
40b7e5d39d
commit
4cf6828536
@ -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',
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user