在做项目的时候用到Element UI中的 upload属性上传图片,但是发现请求之后,只是页面中上传成功,服务器返回的状态码并不是200,而且提示无效token,但是在全局配置的时候已经配置了发起请求时,自动携带token:
/ 请求时, 自动携带token值
axios.interceptors.request.use(config => {
config.headers.Authorization = window.sessionStorage.getItem('token')
// 必须 return config
return config
})
经过几番查找才知道,upload中,已经封装了自己的请求方式,并没有用到上面的axios
所以在Element UI中还提供了一个属性headers,它接受一个对象,里面来设置用自己定义的请求方式,而不用upload自己封装的:
<el-upload
:action="UploadURL"
:on-preview="Preview"
list-type="picture"
:headers="headerOBJ"
>
<el-button size="small" type="primary">点击上传</el-button>
</el-upload>
headerOBJ: {
Authorization: window.sessionStorage.getItem('token')
},