编写H5页面的HTML、CSS和JavaScript代码,包括输入框、按钮等元素,以及相关的样式和交互逻辑。 在JavaScript代码中添加登录验证的逻辑,通过Ajax请求将用户名和密码发送到服务器端进行验证,并在控制台输出登录的检验日志。如果验证成功,则跳转到门户页或待办列表页;如果验证失败,则提示密码错误。
时间: 2024-02-03 17:12:36 浏览: 24
HTML代码:
```
<!DOCTYPE html>
<html>
<head>
<title>Login Page</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
<h1>Login</h1>
<form>
<label for="username">Username:</label>
<input type="text" id="username" name="username" required>
<label for="password">Password:</label>
<input type="password" id="password" name="password" required>
<button type="submit" id="submit-btn">Login</button>
</form>
</div>
<script src="script.js"></script>
</body>
</html>
```
CSS代码:
```
body {
background-color: #f2f2f2;
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
}
.container {
background-color: #fff;
border-radius: 5px;
box-shadow: 0 2px 5px rgba(0,0,0,0.3);
margin: 50px auto;
padding: 20px;
width: 400px;
max-width: 100%;
}
h1 {
margin-top: 0;
}
form {
display: flex;
flex-direction: column;
}
label {
margin-bottom: 5px;
}
input {
border: 1px solid #ccc;
border-radius: 3px;
font-size: 16px;
padding: 10px;
margin-bottom: 10px;
}
#submit-btn {
background-color: #4CAF50;
border: none;
color: #fff;
cursor: pointer;
font-size: 16px;
padding: 10px;
transition: background-color 0.3s;
}
#submit-btn:hover {
background-color: #3e8e41;
}
```
JavaScript代码:
```
const form = document.querySelector('form');
form.addEventListener('submit', (event) => {
event.preventDefault();
const username = document.querySelector('#username').value;
const password = document.querySelector('#password').value;
// Send login request to server
const xhr = new XMLHttpRequest();
xhr.open('POST', '/login');
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.onload = () => {
if (xhr.status === 200) {
console.log('Login success!');
// Redirect to portal page or task list page
window.location.href = '/portal';
} else {
console.log('Login failed. Incorrect password.');
alert('Incorrect password.');
}
};
xhr.send(JSON.stringify({username, password}));
});
```
在代码中,我们首先获取了表单元素和输入框的值。然后创建了一个XMLHttpRequest对象,通过POST请求将用户名和密码以JSON格式发送到服务器端进行验证。如果验证成功,则在控制台输出登录成功的信息并跳转到门户页或待办列表页;如果验证失败,则在控制台输出登录失败的信息并提示密码错误。