使用的是
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>