为了演示预约门票逆向协议的部分代码,我们可以假设有一个简单的流程,用户通过前端表单提交数据,后端处理请求并返回一个预约确认信息。这段代码会展示前端和后端的基本实现。
### 前端 (HTML + JavaScript)
首先,我们创建一个简单的前端表单,用户可以在这里输入他们的姓名和想要预约的时间,然后提交表单。
```html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>预约门票</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 50px;
}
form {
max-width: 300px;
margin: 0 auto;
}
label {
display: block;
margin: 10px 0 5px;
}
input[type="text"], input[type="datetime-local"] {
width: 100%;
padding: 8px;
margin-bottom: 10px;
}
input[type="submit"] {
padding: 10px 20px;
background-color: #4CAF50;
color: white;
border: none;
cursor: pointer;
}
input[type="submit"]:hover {
background-color: #45a049;
}
</style>
</head>
<body>
<h1>预约门票</h1>
<form id="reservationForm">
<label for="name">姓名:</label>
<input type="text" id="name" name="name" required>
<label for="datetime">预约时间:</label>
<input type="datetime-local" id="datetime" name="datetime" required>
<input type="submit" value="提交预约">
</form>
<div id="confirmation"></div>
<script>
document.getElementById('reservationForm').addEventListener('submit', function(event) {
event.preventDefault();
const name = document.getElementById('name').value;
const datetime = document.getElementById('datetime').value;
fetch('http://localhost:3000/reserve', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ name, datetime })
})
.then(response => response.json())
.then(data => {
document.getElementById('confirmation').innerText = `预约成功!确认码: ${data.confirmationCode}`;
})
.catch(error => {
document.getElementById('confirmation').innerText = `预约失败: ${error}`;
});
});
</script>
</body>
</html>