function login(username, password,userType) {
var username = document.getElementById('userName').value;
var password = document.getElementById('password').value;
var userType = document.getElementById('userType').value;
fetch('http://localhost:8088/login', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
userName: username,
passWord: password,
userType:userType
})
})
.then(response => response.json())
.then(data => {
if (data.code === 0 ) {
localStorage.setItem('token', data.data.token);
if (userType === 'Doctor') {
localStorage.setItem('username', username);
window.location.href = 'index.html';
} else if (userType === 'MedicalStaff') {
localStorage.setItem('username', username);
window.location.href = 'index2.html';
}
}else {
if (data.code === -1) {
alert('用户名密码亦或者用户类型输入错误');
} else {
alert('登录失败: ' + response.message);
}
}
})
.catch(error => {
console.error('登录请求失败:', error);
});
}
这是前端登录的接口代码
这是后端提供的接口
根据后端的限制前端的接口也会发生变化
标签:username,js,userType,html,localStorage,go,password,data From: https://blog.csdn.net/weixin_64011437/article/details/139897970