Я работаю над приложением Flask в Python. Я пытаюсь использовать кнопку, которая отправит запрос сообщения при нажатии. Я хочу, чтобы он перешел по маршруту. /> Вот мой код: < /p>
{% extends "base.html" %}
{% block title%}Collection{% endblock %}
{% block main %}
Your collection is displayed below. Click on the pokéball to view its contents. Delete binders from colletion by using the delete button.
{% for binder in user_data %}
[url={{ url_for(]
[img]{{ binder.img_url }}[/img]
[/url]
Name: {{ binder['name']}}
Totatl Market Value: {{ binder['total_mv']}}
DELETE
{% endfor %}
>
// Script for delete button on the collection page
// Select all buttons with the 'deleteButton' class
document.querySelectorAll('.deleteButton').forEach(function(button) {
button.addEventListener('click', function(event) {
// Prevent the form submission or default behavior of the button
event.preventDefault();
// Get the binder_id from the data attribute
const binder_id = this.getAttribute('binder_id');
// Show the confirmation alert
const userConfirmed = confirm("Are you sure you want to delete this binder?");
if (userConfirmed) {
// If user confirms, send the POST request
fetch('/collection', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ binder_id: binder_id }) // Send binder_id in the body
})
.then(response => response.json())
.then(data => {
// Handle the server's response here
console.log('Success:', data);
alert("Item deleted successfully!");
})
.catch(error => {
// Handle any errors
console.error('Error:', error);
});
} else {
// If the user cancels, do nothing
console.log("Delete action canceled.");
}
});
});
{% endblock %}
Я работаю над приложением Flask в Python. Я пытаюсь использовать кнопку, которая отправит запрос сообщения при нажатии. Я хочу, чтобы он перешел по маршруту. /> Вот мой код: < /p> [code]{% extends "base.html" %}
{% block title%}Collection{% endblock %}
{% block main %}
Your collection is displayed below. Click on the pokéball to view its contents. Delete binders from colletion by using the delete button.
// Script for delete button on the collection page // Select all buttons with the 'deleteButton' class document.querySelectorAll('.deleteButton').forEach(function(button) { button.addEventListener('click', function(event) { // Prevent the form submission or default behavior of the button event.preventDefault();
// Get the binder_id from the data attribute const binder_id = this.getAttribute('binder_id');
// Show the confirmation alert const userConfirmed = confirm("Are you sure you want to delete this binder?");
if (userConfirmed) { // If user confirms, send the POST request fetch('/collection', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ binder_id: binder_id }) // Send binder_id in the body }) .then(response => response.json()) .then(data => { // Handle the server's response here console.log('Success:', data); alert("Item deleted successfully!"); }) .catch(error => { // Handle any errors console.error('Error:', error); }); } else { // If the user cancels, do nothing console.log("Delete action canceled."); } }); });
{% endblock %} [/code] Почему эти данные не отправляются?