document.addEventListener('DOMContentLoaded', function() { const registerForm = document.getElementById('register-form'); registerForm.addEventListener('submit', function(event) { event.preventDefault(); // 阻止表单默认提交行为 const username = document.getElementById('username').value; const email = document.getElementById('email').value; const password = document.getElementById('password').value; const confirmPassword = document.getElementById('confirm-password').value; if (password !== confirmPassword) { alert('密码和确认密码不一致'); return; } fetch('/register_post', { method: 'POST', headers: { 'Content-Type': 'application/json', 'X-CSRFToken': '{{ csrf_token() }}' // 如果使用 CSRF 保护 }, body: JSON.stringify({ username, email, password }) }) .then(response => response.json()) .then(data => { if (data.success) { alert('注册成功'); window.location.href = '/login'; // 注册成功后跳转到登录页面 } else { alert('注册失败: ' + data.message); } }) .catch(error => { console.error('Error:', error); alert('注册失败'); }); }); });