JavaScript 签到功能是一种常见的 Web 应用程序功能,用于记录用户的签到信息。下面是关于 JavaScript 签到功能的基础概念、优势、类型、应用场景以及常见问题及解决方法。
签到功能通常涉及以下步骤:
以下是一个简单的 JavaScript 签到功能的示例代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>签到页面</title>
</head>
<body>
<button id="signInBtn">签到</button>
<script>
document.getElementById('signInBtn').addEventListener('click', function() {
fetch('/api/signin', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ userId: '12345' })
})
.then(response => response.json())
.then(data => {
if (data.success) {
alert('签到成功!');
} else {
alert('签到失败:' + data.message);
}
})
.catch(error => {
console.error('Error:', error);
alert('网络错误,请稍后再试。');
});
});
</script>
</body>
</html>const express = require('express');
const app = express();
app.use(express.json());
const signInRecords = {}; // 模拟签到记录存储
app.post('/api/signin', (req, res) => {
const userId = req.body.userId;
if (!userId) {
return res.status(400).json({ success: false, message: '用户ID不能为空' });
}
if (!signInRecords[userId]) {
signInRecords[userId] = [];
}
const today = new Date().toISOString().split('T')[0];
if (signInRecords[userId].includes(today)) {
return res.json({ success: false, message: '今日已签到' });
}
signInRecords[userId].push(today);
res.json({ success: true, message: '签到成功' });
});
app.listen(3000, () => {
console.log('Server is running on port 3000');
});希望这些信息对你有所帮助!如果有更多具体问题,请随时提问。
没有搜到相关的文章