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>
);
}
}
问题是什么 ?谢谢
解决方案
你可以在 axios 中设置 headers 如下:
const headers = {
'Content-Type': 'application/json',
'X-Auth-Token': '97e0d315477f435489cf04904c9d0e6co',
};
axios.get(url, {headers})
推荐阅读
- python - 将图像读取到预先分配的 numpy 数组
- javascript - ES6 Modules - 导入文件的 3 种方式
- javascript - 最新 API 中 GuideDropDownList 中的 Item 值更改是否有任何回调函数?
- javascript - 我如何在 ngFor-inserted 上使用 d3.select()
的? - android - Android Studio 不断显示我以前的 Android 应用
- react-redux - 自定义渲染连接反应组件与模拟 axios 响应 - getBy* 查询误导性异常
- python - 如何从 python selenium 数据构建动态表?
- php - 升级到 1.32 后 rev_timestamp 无效
- regex - 打印标签的内容
- r - 如何修复此错误“rep 中的错误(是,length.out = len):尝试复制类型为 'S4' 的对象”