html-re/public/script.js
2024-07-13 15:26:57 +08:00

71 lines
2.1 KiB
JavaScript

$(document).ready(function() {
// 切换到注册表单
$('#signUp').click(function() {
$('.container').addClass('right-panel-active');
});
// 切换到登录表单
$('#signIn').click(function() {
$('.container').removeClass('right-panel-active');
});
// 处理注册表单提交
$('#form1').submit(function(event) {
event.preventDefault();
const username = $('#register-username').val().trim();
const password = $('#register-password').val().trim();
if (!username || !password) {
alert('用户名和密码不能为空');
return;
}
$.ajax({
type: 'POST',
url: '/api/register',
contentType: 'application/json',
data: JSON.stringify({ username, password }),
success: function(response) {
if (response.success) {
alert('注册成功,请登录');
$('#signIn').click(); // 切换到登录表单
} else {
alert(response.message);
}
},
error: function() {
alert('网络错误');
}
});
});
// 处理登录表单提交
$('#form2').submit(function(event) {
event.preventDefault();
const username = $('#login-username').val().trim();
const password = $('#login-password').val().trim();
if (!username || !password) {
alert('用户名和密码不能为空');
return;
}
$.ajax({
type: 'POST',
url: '/api/login',
contentType: 'application/json',
data: JSON.stringify({ username, password }),
success: function(response) {
if (response.success) {
window.location.href = '/main.html';
} else {
alert(response.message);
}
},
error: function() {
alert('网络错误');
}
});
});
});