index.html 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>插入用户数据</title>
  6. </head>
  7. <body>
  8. <h1>插入用户数据</h1>
  9. <form id="userForm">
  10. <button type="submit">提交</button>
  11. </form>
  12. <div id="result"></div>
  13. <script>
  14. document.getElementById('userForm').addEventListener('submit', async function(e) {
  15. e.preventDefault();
  16. const name = document.getElementById('name').value;
  17. const age = document.getElementById('age').value;
  18. const resultDiv = document.getElementById('result');
  19. try {
  20. const res = await fetch('http://localhost:3000/api/insert', {
  21. method: 'POST',
  22. headers: { 'Content-Type': 'application/json' },
  23. body: JSON.stringify({ name, age })
  24. });
  25. const data = await res.json();
  26. if (data.success) {
  27. resultDiv.textContent = '插入成功,ID: ' + data.id;
  28. } else {
  29. resultDiv.textContent = '插入失败: ' + (data.error || '未知错误');
  30. }
  31. } catch (err) {
  32. resultDiv.textContent = '请求出错: ' + err;
  33. }
  34. });
  35. </script>
  36. </body>
  37. </html>