【问题标题】:Axios call to Springboot backend responding is not forwarding to new page after returning responseAxios调用Springboot后端响应返回响应后未转发到新页面
【发布时间】:2021-03-18 16:36:46
【问题描述】:

我的控制器中的后映射“登录”返回 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);
        })
  }

Response from dev tools

Response from test

【问题讨论】:

  • 哪一行记录了未定义的内容?
  • @aksappy console.log(err) 和其他我可以告诉的
  • 你能在这里添加一个错误,如果可以的话,请在 then 块的乞求时控制台记录状态码?
  • 在你的 if 条件之前添加这一行 - console.log(res) - 请告诉我们你得到了什么
  • @aksappy 在“测试响应”下完成。它回复了我的电子邮件和正确的(加密的)密码字符串

标签: javascript vue.js axios


【解决方案1】:

Axios 响应架构文档是 here

除非您的控制器响应中有键 code,否则 response.data.code 将是未定义的。

如果您想检查 HTTP 状态,请改用 res.status

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多