实现登录功能,跳转到对应角色的页面
页面还是沿用之前课程的模版样式
const url = `http://localhost:8080/oldMan/Login/${username}/${password}`;
// 发送 AJAX 请求
fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
}
})
.then(response => {
if (!response.ok) {
throw new Error('网络请求失败');
}
return response.json();
})
.then(data => {
console.log('完整响应数据:', data);
if (data.code === 1) { // 根据后端实际的成功状态码判断
const position = data.data.position;
switch (position) {
case '监护人':
window.location.href = '/guardian.html';
break;
case '评估员':
window.location.href = '/assessor.html';
break;
default:
alert('未知职位,登录失败');
}
} else {
alert(data.msg); // 显示登录失败信息
}
})
.catch(error => {
console.error('登录请求出错:', error);
alert('登录请求出错,请稍后重试');
});
标签:登录,data,系统,alert,error,position,老年人,response,评估
From: https://www.cnblogs.com/wjhfree/p/18708094