54 lines
1.9 KiB
HTML
54 lines
1.9 KiB
HTML
<!DOCTYPE html>
|
||
<html lang="zh-TW" xmlns="http://www.w3.org/1999/html" xmlns="http://www.w3.org/1999/html">
|
||
<head>
|
||
<!-- Required meta tags -->
|
||
<meta charset="utf-8">
|
||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||
|
||
<title>註冊</title>
|
||
</head>
|
||
<body>
|
||
<form id="register-form">
|
||
|
||
<p><input type="text" id="username" placeholder="Username"></p>
|
||
<p><input type="password" id="password" placeholder="Password" autocomplete="true"></p>
|
||
<p><button type="submit">Login</button></p>
|
||
</form>
|
||
|
||
<script src="https://cdn.jsdelivr.net/npm/axios@1.1.2/dist/axios.min.js"></script>
|
||
<script language="javascript" type="text/javascript">
|
||
document.getElementById('register-form').addEventListener('submit',
|
||
function(event) {
|
||
event.preventDefault(); // 阻止表单的默认提交行为
|
||
|
||
var username = document.getElementById('username').value;
|
||
var password = document.getElementById('password').value;
|
||
|
||
// 执行登录请求
|
||
register(username, password);
|
||
});
|
||
|
||
function register(username, password) {
|
||
axios.post('./api/v1/register', {
|
||
account: username,
|
||
password: password
|
||
})
|
||
.then(function(response) {
|
||
var token = response.data.token; // 假设服务器返回一个名为"token"的字段
|
||
|
||
// 将JWT存储到本地(通常是使用localStorage)
|
||
localStorage.setItem('jwtToken', token);
|
||
|
||
// 登录成功后,进行其他操作(例如重定向到受保护的页面)
|
||
window.location.href = './index.html';
|
||
})
|
||
.catch(function(error) {
|
||
const message = error.response.data.message;
|
||
alert('註冊失敗: 此帳號已經存在');
|
||
//console.error('註冊失敗:', message);
|
||
});
|
||
}
|
||
</script>
|
||
|
||
</body>
|
||
</html> |