如何使用 fetch 重写此方法?

问题描述

在我的 React 应用中,我使用 axios 向服务器发送请求。

  // This method will get the data from the database.
  componentDidMount() {
    axios
      .get("http://localhost:3000/record")
      .then((response) => {
        this.setState({ records: response.data });
      })
      .catch(function (error) {
        console.log(error);
      });
  }

如何使用 fetch 重写此方法

解决方法

fetch('http://localhost:3000/record')
  .then(response => {
        this.setState({ records: response.data });
      })
  .catch(function (error) {
        console.log(error);
      });;

如果您想使用其他请求方法,请选中此 link