【问题标题】:Unable to add Axios Default Headers to VueJS app无法将 Axios 默认标头添加到 VueJS 应用程序
【发布时间】:2020-11-04 05:33:09
【问题描述】:
我试过了
axios.defaults.headers.common['X-CSRF-TOKEN'] = 'test';
axios.defaults.headers.common['Authenticaton'] = 'Bearer slskdf';
在我的路由器文件中的 main.js 和 index.js 中,beforeEach.....根本没有标题出现在浏览器中。
让这些标头出现在每次通话中我做错了什么?
谢谢
【问题讨论】:
标签:
vue.js
axios
http-headers
jwt
【解决方案1】:
从 0.19 版开始,如果您创建 axios 实例来发出请求,axios 将不再使用 axios.defaults。为实例设置默认值并使用它来发出请求。
const adapter = axios.create({
baseURL: 'http://localhost:8080/my-api',
headers: {
Accept: 'application/json;charset=UTF-8',
},
});
adapter.defaults.headers.common['Authorization'] = 'Bearer test';
adapter.get(...)
See [this issue][1] and [related pull request][1]