data-platform/main.js

49 lines
1.6 KiB
JavaScript
Raw Permalink Normal View History

2024-01-09 21:12:04 +08:00
// 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);
// });
// });