记录 | Vue报错:TypeError: Cannot set properties of undefined (setting ‘Authorization‘)
TypeError: Cannot set properties of undefined (setting ‘Authorization‘)
·
参赛话题:学习笔记
报错:
TypeError: Cannot set properties of undefined (setting 'Authorization')
报错原因:
响应拦截器写错,将前面的请求拦截器覆盖了
instance.interceptors.request.use(config => {
if (Cookies.isKey("token")) {
config.headers.Authorization = Cookies.get("token")
} else {
router.push("/login")
}
return config
})
instance.interceptors.request.use(res => {
// 这边响应拦截器写成了请求拦截器
if ((res.status || res.data.code) == 401) {
alert("请先登录")
router.push("/login")
}
return res.data
}, err => {
return Promise.reject(err)
})
修改之后就正常了
instance.interceptors.request.use(config => {
if (Cookies.isKey("token")) {
config.headers.Authorization = Cookies.get("token")
} else {
router.push("/login")
}
return config
})
instance.interceptors.response.use(res => {
// response
if ((res.status || res.data.code) == 401) {
alert("请先登录")
router.push("/login")
}
return res.data
}, err => {
return Promise.reject(err)
})
更多推荐
已为社区贡献3条内容
所有评论(0)