【问题标题】:Axios is not outputting Console.log but the JSON data is being returnedAxios 没有输出 Console.log 但正在返回 JSON 数据
【发布时间】:2018-01-18 05:30:12
【问题描述】:

为什么我不能从 API 获取 JSON 响应?我有这个函数向我的 API 发出 axios POST 请求。

handleSubmit(event) {
    axios.post('/api/v1/user_token/',{
      auth: {
        email: this.state.email,
        password: this.state.password
      }
    })
  .then(function(response) {
    console.log(response.data);
    console.log(response.statusText);
  })
  .catch(function (error) {
    console.log(error);
  })
}

它在响应正文中返回正确的 JsonWebToken。但是,当我尝试使用它设置 cookie 或将其输出到 console.log 时,例如console.log(response.data),我在输出中什么也看不到。附上相关图片。

JWT returning as expected

But no output in the console

【问题讨论】:

  • 你用postman试过你的api吗...
  • 似乎端点工作只是返回一个用户令牌,仅此而已。它似乎不应该返回任何数据。
  • 我会试试邮递员,因为我看到令牌返回。但是如果我无法以某种方式捕获它,我不知道如何将其设置为 Cookie。

标签: api reactjs axios


【解决方案1】:

我只是在响应中输入了错误的命名空间。最终,我坚持能够通过在函数中包含 e.preventDefault() 来注销所有内容。

  handleSubmit(e) {
      e.preventDefault();
      axios.post('/api/v1/user_token/',
        {auth: {
          email: this.state.email,
          password: this.state.password
        }
      })
      .then(function(response) {
        if (response.status !== 201) {
          console.log('Authentication failed.' + response.status);
        }
        return response.data;
      })
  }

【讨论】:

    猜你喜欢
    • 2021-10-26
    • 2022-01-11
    • 2022-01-11
    • 1970-01-01
    • 2013-10-31
    • 1970-01-01
    • 2020-09-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多