JavaScript отправляет данные JSON [закрыто]Javascript

Форум по Javascript
Ответить
Anonymous
 JavaScript отправляет данные JSON [закрыто]

Сообщение Anonymous »

Я работаю над приложением 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 %}
Почему эти данные не отправляются?

Подробнее здесь: https://stackoverflow.com/questions/794 ... -json-data
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Javascript»