ADD file via upload

This commit is contained in:
p73692015 2024-01-09 21:12:04 +08:00
parent 650d8c3361
commit 0bc596d587
1 changed files with 48 additions and 0 deletions

48
main.js Normal file
View File

@ -0,0 +1,48 @@
// frontend/main.js
function addTodo() {
const todoList = document.getElementById('todoList');
const todoInput = document.getElementById('todoInput');
const todoText = todoInput.value;
fetch('http://127.0.0.1:5000/todos',{
method: 'POST',
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({text: todoText}),
})
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok! response='+response.status);
}
return response.text(); // 从响应获取文本数据
})
.then(newTodo => {
const todoItem = document.createElement('li');
todoItem.textContent = newTodo.text;
todoList.appendChild(todoItem);
todoInput.value = '';
})
.then(data => {
try {
const jsonData = JSON.parse(data); // 尝试解析响应数据
console.log('JSON data:', jsonData);
} catch (error) {
console.error('Error parsing JSON:', error);
}
})
.catch(error => {
console.error('Error:', error);
});
document.getElementById("todoList").innerHTML = todoText;
}
// fetch('http://127.0.0.1:5000',{mode: 'no-cors'})
// .then(response => response.json())
// .then(todos => {
// todos.forEach(todo => {
// const todoItem = document.createElement('li');
// todoItem.textContent = todo.text;
// todoList.appendChild(todoItem);
// });
// });