我是 Vue.js 新手,想要在组件中向受限 api 发出请求:
computed: {
token () {
return this.$store.getters.getToken;
},
...
created () {
axios
.get( this.BASE_URL + '/profile/me')
.then( res => {
this.profile = res.data;
console.log('profile is:', res.data);
})
.catch(error => console.log(error))
},
问题是我不知道如何将令牌包含到请求标头中。所以毫不奇怪我得到401
响应错误。
当我尝试时
axios.defaults.headers.common['Authorization'] = this.token;
在我收到 get 请求之前OPTIONS /profile/me
代替GET /profile/me
在服务器日志中。
我该如何修复它?
Axios get()
请求接受两个参数。因此,除了 url 之外,您还可以在其中放入 JWT。
axios.get(yourURL, yourConfig)
.then(...)
在你的情况下yourConfig
可能是这样的
yourConfig = {
headers: {
Authorization: "Bearer " + yourJWTToken
}
}
您还可以在此处阅读有关可以在配置中添加哪些内容的信息https://github.com/axios/axios https://github.com/axios/axios。
只需搜索“请求配置”
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)