From 0bc596d587b02c0aef0fd743986e99e7a8b41145 Mon Sep 17 00:00:00 2001 From: p73692015 Date: Tue, 9 Jan 2024 21:12:04 +0800 Subject: [PATCH] ADD file via upload --- main.js | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 main.js diff --git a/main.js b/main.js new file mode 100644 index 0000000..a159900 --- /dev/null +++ b/main.js @@ -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); +// }); +// });