vue.js - axios.post() 后没有响应
问题描述
嘿,我正在我的 vue 项目上开发登录系统,但问题是后端似乎没有响应。
这是后端功能:
auth.post('/login', async function (req, res) {
const { email, password } = req.body;
console.log(req);
if(email !== "" && password !== "") {
const account = await User.findOne({ where: { email: email} });
if (account) {
if (await account.validPassword(password)) {
// Generate an access token
const accessToken = jwt.sign({ id: account.id }, SECRET);
const account_data =
{
'id': account.id,
'firstName': account.firstName,
'lastName': account.lastName,
'email': account.email,
'isAdmin': account.isAdmin
}
res.send({accessToken, account_data});
} else {
res.status(200).json("Username or password incorrect");
}
} else {
res.send('Username or password incorrect');
}
} else {
res.send('Username or password incorrect');
}
})
这是我调用动作的方法
methods: {
async loginUser(){
let user = await this.$store.dispatch('loginUser', this.loginInfo);
if(user.error){
alert(user.error)
} else {
alert('Thank you for signing in, ' + user.firstName);
}
},
}
这是动作:
async loginUser({commit}, loginInfo){
console.log(loginInfo)
try{
let response = await axios({
method: 'POST',
url: 'http://localhost:4000/api/auth/login',
data: loginInfo,
headers: {
// Overwrite Axios's automatically set Content-Type
'Content-Type': 'application/json'
}});
let user = response.data;
console.log(user);
commit('SET_CURRENT_USER', user);
} catch (e){
alert(e);
return {e}
}
}
console.log
函数内try
或函数内都catch
不会被触发。
解决方案
推荐阅读
- terraform - 在 azure 中运行的每个管道的负载均衡器后端池下获取额外的 VM
- typescript - 利用接口或类型来描述对象的键
- python - 由用户对 Dataframe 列执行计算并添加为新列
- php - 使用标志时 MySQL 连接被拒绝
- python - Apache SPark:groupby 未按预期工作
- amazon-web-services - 亚马逊个性化 SIMS 和个性化排名食谱是否支持元数据?
- html - 修复了具有全窗口拉伸和滚动时不重复的背景图像?
- r - R:如何在df中的5个变量中逐行找到10个最常见的对
- python - Python ModuleNotFound 等
- kotlin - 我应该为一个小列表使用 Android 分页库吗?