login.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. document.addEventListener('DOMContentLoaded', () => {
  2. fetch('http://127.0.0.1:8080/api/image')
  3. .then(response => response.blob())
  4. .then(imageBlob => {
  5. const imageObjectURL = URL.createObjectURL(imageBlob);
  6. document.getElementById('logo').src = imageObjectURL;
  7. })
  8. .catch(error => {
  9. console.error('Error fetching logo:', error);
  10. });
  11. });
  12. async function login(event) {
  13. event.preventDefault(); // 阻止表单的默认提交行为
  14. const Account = document.getElementById('Account').value;
  15. const password = document.getElementById('password').value;
  16. try {
  17. const response = await fetch('http://127.0.0.1:8080/api/login', {
  18. method: 'POST',
  19. headers: {
  20. 'Content-Type': 'application/json'
  21. },
  22. body: JSON.stringify({ Account, password })
  23. });
  24. if (!response.ok) {
  25. const errorText = await response.text();
  26. throw new Error(errorText || response.statusText);
  27. }
  28. const result = await response.json();
  29. const token = result.token; // 假设服务器返回的 JSON 中包含一个 token 字段
  30. localStorage.setItem('Authorization', token);
  31. // 登录成功后重定向到 license_info.html 页面
  32. window.location.href = '/static/license_info/license_info.html';
  33. } catch (error) {
  34. alert('登录失败: ' + error.message);
  35. }
  36. }