使用的是
Content-Type
application/x-www-form-urlencoded形式
如
代碼如下的html,后端沒寫下去:
<!DOCTYPE html>
<html lang="zh-CN">
<head><meta charset="UTF-8"><title>管理員登錄</title><style>body { font-family: Arial; margin: 40px; }input { width: 200px; padding: 5px; margin: 5px; }button { padding: 5px 10px; margin: 5px; }#msg { margin-top: 10px; color: red; }</style>
</head>
<body>
<h1>管理員登錄</h1>
<input type="text" id="username" placeholder="用戶名">
<input type="password" id="password" placeholder="密碼">
<button onclick="login()">登錄</button>
<p id="msg"></p><script>function login() {const username = document.getElementById('username').value;const password = document.getElementById('password').value;if(!username || !password) {document.getElementById('msg').innerText = '用戶名或密碼不能為空';return;}fetch('/admin/login', {method: 'POST',headers: { 'Content-Type': 'application/x-www-form-urlencoded' },body: `username=${encodeURIComponent(username)}&password=${encodeURIComponent(password)}`}).then(res => res.text()).then(msg => {document.getElementById('msg').innerText = msg;});}
</script>
</body>
</html>