如何使用axios设置标题

jim*_*m2k 0 api reactjs axios

我试图从我发现的API足球中获取信息。在邮递员上,它运作良好,但我无法使用axios正确设置标题。

这是我的代码:

class App extends React.Component {
  constructor(props) {
    super(props);
  }

  getTest = (e) => {
    e.preventDefault();
    let headers = {
      'Content-Type': 'application/json',
      'Access-Control-Allow-Origin': '*',
      'X-Auth-Token': '97e0d315477f435489cf04904c9d0e6co',
    };

    axios.get("https://api.football-data.org/v2/teams/90/matches?status=SCHEDULED", {headers})
      .then(res => {
        console.log("DATA")
      })
      .catch(res => {
        console.log("ERR", res);
      });
  }

    render() {
    return (
      <div>
        <button onClick={this.getTest}>info</button>
      </div>
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

问题是什么 ?谢谢

Uma*_*ooq 7

您可以在axios中设置标头,如下所示:

const headers = {
  'Content-Type': 'application/json',
  'X-Auth-Token': '97e0d315477f435489cf04904c9d0e6co',
};
axios.get(url, {headers})
Run Code Online (Sandbox Code Playgroud)