<分区>
我是 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))
},
问题是我不知道如何将 token 包含到请求 header 中。所以我收到 401
错误响应也就不足为奇了。
当我尝试的时候
axios.defaults.headers.common['Authorization'] = this.token;
在获取请求之前,我在服务器日志中收到 OPTIONS/profile/me
而不是 GET/profile/me
。
我该如何解决?