Use base.js CSRF auto-injection for AJAX requests
- Remove manual CSRF handling from JavaScript - base.js already intercepts fetch() and adds X-CSRF-TOKEN automatically - Simplify JavaScript code for subtasks operations
This commit is contained in:
@@ -227,13 +227,6 @@
|
||||
{% block scripts %}
|
||||
{{ parent() }}
|
||||
<script>
|
||||
function getCsrfData() {
|
||||
return {
|
||||
token: '{{ csrf_token() }}',
|
||||
hash: '{{ csrf_hash() }}'
|
||||
};
|
||||
}
|
||||
|
||||
function addSubtask(event, taskId) {
|
||||
event.preventDefault();
|
||||
const form = event.target;
|
||||
@@ -242,15 +235,12 @@ function addSubtask(event, taskId) {
|
||||
|
||||
if (!title) return;
|
||||
|
||||
const csrf = getCsrfData();
|
||||
|
||||
fetch(`/tasks/${taskId}/subtasks`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'X-Requested-With': 'XMLHttpRequest',
|
||||
'Content-Type': 'application/x-www-form-urlencoded',
|
||||
},
|
||||
body: `title=${encodeURIComponent(title)}&${csrf.token}=${csrf.hash}`
|
||||
body: 'title=' + encodeURIComponent(title)
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
@@ -299,15 +289,11 @@ function updateSubtasksCount() {
|
||||
}
|
||||
|
||||
function toggleSubtask(taskId, subtaskId) {
|
||||
const csrf = getCsrfData();
|
||||
|
||||
fetch(`/tasks/${taskId}/subtasks/${subtaskId}/toggle`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'X-Requested-With': 'XMLHttpRequest',
|
||||
'Content-Type': 'application/x-www-form-urlencoded',
|
||||
},
|
||||
body: `${csrf.token}=${csrf.hash}`
|
||||
}
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
@@ -328,15 +314,11 @@ function toggleSubtask(taskId, subtaskId) {
|
||||
function deleteSubtask(taskId, subtaskId) {
|
||||
if (!confirm('Удалить подзадачу?')) return;
|
||||
|
||||
const csrf = getCsrfData();
|
||||
|
||||
fetch(`/tasks/${taskId}/subtasks/${subtaskId}/delete`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'X-Requested-With': 'XMLHttpRequest',
|
||||
'Content-Type': 'application/x-www-form-urlencoded',
|
||||
},
|
||||
body: `${csrf.token}=${csrf.hash}`
|
||||
}
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
|
||||
Reference in New Issue
Block a user