- 修改 .gitignore 文件,添加新忽略的目录 - 在 package.json 中添加 better-sqlite3 依赖 - 更新 index.html,改进登录表单,添加注册按钮 - 优化 login.js 中的 fetch 请求和错误处理
66 lines
2.0 KiB
HTML
66 lines
2.0 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Login Form</title>
|
|
</head>
|
|
<body>
|
|
<form id="loginForm">
|
|
Username: <input type="text" name="username"><br>
|
|
Password: <input type="text" name="password"><br>
|
|
<input type="submit" value="Login">
|
|
<input type="submit" value="Sign">
|
|
</form>
|
|
|
|
<script>
|
|
document.getElementById('loginForm').addEventListener('submit', function(event) {
|
|
event.preventDefault(); // 阻止表单默认提交行为
|
|
|
|
const username = document.querySelector('input[name="username"]').value;
|
|
const password = document.querySelector('input[name="password"]').value;
|
|
|
|
const bodyData = new URLSearchParams({
|
|
username: username,
|
|
password: password
|
|
});
|
|
|
|
console.log(bodyData.toString());
|
|
|
|
fetch('/login', {
|
|
method: 'POST',
|
|
body: bodyData.toString(), // 发送表单数据
|
|
headers: {
|
|
'Content-Type': 'application/x-www-form-urlencoded' // 设置请求头
|
|
}
|
|
})
|
|
.then(response => {
|
|
if (response.ok) {
|
|
return response.json();
|
|
} else {
|
|
throw new Error('Login failed');
|
|
}
|
|
})
|
|
.then(data => {
|
|
if (data.success) {
|
|
window.location.href = data.redirectUrl; // 跳转到指定URL
|
|
} else {
|
|
alert('Login failed');
|
|
}
|
|
})
|
|
.catch(error => {
|
|
console.error('Error:', error);
|
|
alert('An error occurred during login');
|
|
});
|
|
// try {
|
|
|
|
// {
|
|
// throw new Error('sfda');
|
|
// }
|
|
// } catch (error) {
|
|
|
|
// }
|
|
});
|
|
</script>
|
|
</body>
|
|
</html> |