javascript - Axios调用Springboot后端响应返回响应后不转发到新页面
问题描述
我的控制器中的后映射“登录”返回 200。但我越来越不确定,我相信它来自我的 axios 调用。我知道控制台中的 catch 块正在报告未定义的错误
Axios 调用 -
submit() {
let formData = new FormData();
formData.set("email", this.email)
formData.set("password", this.password)
formData.set("staySignedIn", this.staySignedIn)
// When the client/server sides are running from the same port (AWS) the url for this api call should be changed to /api/v1/login
axios.post("http://localhost:8080/api/v1/login", formData,
{headers: {'Content-Type': 'application/json'}})
.then(function (res) {
console.log(res); // test
if (res.data.code === 200) {
this.router.push('/dashboard')
console.log("success");
} else {
console.log(res.data.code);
}
})
.catch(function (err) {
console.log(err);
})
}
解决方案
Axios 响应架构文档在这里
code
除非您在控制器响应中有键,否则response.data.code
将是未定义的。
res.status
如果您想检查 HTTP 状态,请尝试改用。
axios.post("http://localhost:8080/api/v1/login", formData,
{headers: {'Content-Type': 'application/json'}})
.then(function (res) {
if (res.status === 200) {
this.router.push('/dashboard')
console.log("success");
} else {
console.log(res.status);
}
})
.catch(function (err) {
console.log(err);
})
编辑 您似乎在回复中发回了密码。即使密码是加密的,最好限制在响应中公开它。
推荐阅读
- android - Android Q 访问外部文件夹
- sql - 获取查询 Bigquery 的直方图
- vega-lite - 对相同的 y 值标记点沿所有值使用多项选择
- abcpdf - 使用 AbcPdf 10 设置 pdf 字段值会创建具有错误值的占位符
- r - R中长数据集中每个ID组的最后一行子集
- javascript - Chrome 扩展:在 URL 字符串匹配时取消隐藏弹出文本
- algorithm - 需要安排事件的顺序
- javascript - SAP Cloud SDK JavaScript:具有位置 ID 的 onPremise 目的地没有 CSRF 令牌
- r - 光栅中的反应式表达
- angular - 如何在 NativeScript 中对表单进行单元测试?