如何使用 axios 发出 API 请求?

How to make an API request using axios?

我是 React 初学者,遇到以下问题:

我使用的 API 超过 60 个字符,我的问题是我想确保 API 的所有字符都呈现出来,而不必手动添加每个数字。 ({this.state.people[0].name}, ..., {this.state.people[n].name})。

这个问题还让我进行了两次 API 调用,因为 API 每次调用仅给出 10 个结果。接下来是第 2 页。

我用 ?=page2 创建了一个新调用,但这是一种非常乏味的方法,因为我必须添加 10 个不同的 API 调用才能添加所有字符。 我考虑过做一个 for 循环,但我真的不知道该怎么做。

class Api extends Component {

state ={
    people:[
    {name:'Title'},
    {name:'Title'},
    {name:'Title'},
    {name:'Title'},
    {name:'Title'},
    {name:'Title'},
    {name:'Title'},
    {name:'Title'},
    {name:'Title'},
    {name:'Title'},
    {name:'Title'},
    {name:'Title'},
    {name:'Title'},
    ],

    people2:[
    {name:'Title'},
    {name:'Title'},
    {name:'Title'},
    {name:'Title'},
    {name:'Title'},
    {name:'Title'},
    {name:'Title'},
    {name:'Title'},
    {name:'Title'},
    {name:'Title'},
    {name:'Title'},
    {name:'Title'},
    {name:'Title'},
    ]
}

componentDidMount() {
  axios.get('https://swapi.co/api/people/')

  .then(starwars => {
    this.setState({
        people: starwars.data.results
    })
    console.log(starwars.data.results)

  })
axios.get('https://swapi.co/api/people/?page=2')
  .then(starwars2 => {
    this.setState({
        people2: starwars2.data.results
    })
    console.log(starwars2.data.results)

  })
  .catch(error => {
    console.log(error);
  });
}

  render(){
    return ( 

        <div>
        <Cards title={this.state.people[0].name} />
        <Cards title={this.state.people[1].name} />
        <Cards title={this.state.people[2].name} />
        <Cards title={this.state.people[3].name} />
        <Cards title={this.state.people[4].name} />
        <Cards title={this.state.people[5].name} />
        <Cards title={this.state.people[6].name} />
        <Cards title={this.state.people[7].name} />
        <Cards title={this.state.people[8].name} />
        <Cards title={this.state.people[9].name}  />
        <Cards title={this.state.people2[0].name}  />
        <Cards title={this.state.people2[1].name}  />
        <Cards title={this.state.people2[2].name}  />
        <Cards title={this.state.people2[3].name}  />
        <Cards title={this.state.people2[4].name}  />
        <Cards title={this.state.people2[5].name}  />
        <Cards title={this.state.people2[6].name}  />
        <Cards title={this.state.people2[7].name}  />
        <Cards title={this.state.people2[8].name}  />
        <Cards title={this.state.people2[9].name}  />

</div>)
  }
}
export default Api;


API 一次调用 60 个字符。

正如您所说,您可以通过编程方式循环并获取所有 。为此,您需要知道那里有多少页。请求 https://swapi.co/api/people/ returns 一个 count 作为响应的一部分。假设这是总人数,您可以使用它来计算您需要查询的页面数。它看起来像这样:

注意:我为此使用了 Node,只需添加必要的 React 位即可。

const axios = require("axios");
// first page
axios("https://swapi.co/api/people/")
    .then(starwars => {
        return starwars.data.count;
    })
    .then(count => {
        // exclude the first request
        const numberOfPagesLeft = Math.ceil((count - 1) / 10);
        let promises = [];
        // start at 2 as you already queried the first page
        for (let i = 2; i <= numberOfPagesLeft; i++) {
            promises.push(axios(`https://swapi.co/api/people?page=${i}`));
        }
        return Promise.all(promises);
    })
    .then(response => {
        // collect all results into one array
        const result = response.reduce((acc, data) => [...acc, ...data.data.results], []);
        return result;
        //result should contain the remaining records - pages 2 through n.
    })
    .catch(error => console.log("Properly handle your exception here"));

一如既往,注意 Promise.all

fast-fail 行为