首页 > 解决方案 > 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);
        })
  }

开发工具的响应

测试响应

标签: javascriptvue.jsaxios

解决方案


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);
        })

编辑 您似乎在回复中发回了密码。即使密码是加密的,最好限制在响应中公开它。


推荐阅读