Fix CSRF token in POST body for AJAX requests

- Add csrf_token parameter to fetch() body
- CodeIgniter 4 CSRF filter requires token in POST body, not just headers
This commit is contained in:
Vladimir Tomashevskiy 2026-02-08 16:28:16 +00:00
parent e85390be84
commit a9b08bdc6e
1 changed files with 16 additions and 4 deletions

View File

@ -235,12 +235,15 @@ function addSubtask(event, taskId) {
if (!title) return;
// Получаем CSRF токен из формы
const csrfToken = form.querySelector('input[name="csrf_token"]').value;
fetch(`/tasks/${taskId}/subtasks`, {
method: 'POST',
headers: {
'X-Requested-With': 'XMLHttpRequest',
},
body: 'title=' + encodeURIComponent(title)
body: 'title=' + encodeURIComponent(title) + '&csrf_token=' + csrfToken
})
.then(response => response.json())
.then(data => {
@ -289,11 +292,16 @@ function updateSubtasksCount() {
}
function toggleSubtask(taskId, subtaskId) {
// Получаем CSRF токен из формы на странице
const csrfForm = document.querySelector('form[action*="/tasks/' + taskId + '"]');
const csrfToken = csrfForm ? csrfForm.querySelector('input[name="csrf_token"]').value : '';
fetch(`/tasks/${taskId}/subtasks/${subtaskId}/toggle`, {
method: 'POST',
headers: {
'X-Requested-With': 'XMLHttpRequest',
}
},
body: 'csrf_token=' + csrfToken
})
.then(response => response.json())
.then(data => {
@ -301,7 +309,6 @@ function toggleSubtask(taskId, subtaskId) {
alert(data.error || 'Ошибка');
location.reload();
} else {
// Перезагружаем страницу для обновления UI
location.reload();
}
})
@ -314,11 +321,16 @@ function toggleSubtask(taskId, subtaskId) {
function deleteSubtask(taskId, subtaskId) {
if (!confirm('Удалить подзадачу?')) return;
// Получаем CSRF токен из формы на странице
const csrfForm = document.querySelector('form[action*="/tasks/' + taskId + '"]');
const csrfToken = csrfForm ? csrfForm.querySelector('input[name="csrf_token"]').value : '';
fetch(`/tasks/${taskId}/subtasks/${subtaskId}/delete`, {
method: 'POST',
headers: {
'X-Requested-With': 'XMLHttpRequest',
}
},
body: 'csrf_token=' + csrfToken
})
.then(response => response.json())
.then(data => {