【问题标题】:React js nested axios callReact js 嵌套 axios 调用
【发布时间】:2017-07-06 20:37:09
【问题描述】:

如何进行嵌套 axios 调用?我需要将第一个 axios 调用的结果作为参数传递给第二个 axios

【问题讨论】:

标签: reactjs axios


【解决方案1】:

你可以像普通的承诺一样链接。在第一个请求的 .then 中触发下一个请求,然后返回该承诺,这样您就可以为第二个请求提供一个 .then

axios.get(...)
  .then((response) => {
    return axios.get(...); // using response.data
  })
  .then((response) => {
    console.log('Response', response);
  });

【讨论】:

  • 如果返回请求失败会发生什么,你如何捕捉到那个特定的?
  • @Omar 你可以返回一个带有承诺的catchreturn axios.get(...).catch(...)
  • 我正在连续调用 3 个 API,我使用了上面的代码并且它正在工作,但它在第二个 API 中我正在打印一些东西,但它无法打印,但它正在日志中打印。怎么解决的?
  • @Omar 这在我的情况下不起作用,还有其他解决方案吗?
猜你喜欢
  • 1970-01-01
  • 2020-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-01
  • 1970-01-01
  • 2016-07-25
  • 2020-06-02
相关资源
最近更新 更多