【发布时间】:2020-01-01 13:17:02
【问题描述】:
在我用 VUE 制作的 SPA 中,我正在设置我的 Axios 存储库(或 Api 文件),但我在使用拦截器时遇到了问题。
基本上,如果 jwtoken 已过期或缺少拦截器,我会在调试中看到 401 错误,并且在网络调试中看到对 api 服务器的尝试。
问题是当我得到一个有效的 jwtoken 时。在这种情况下,我没有尝试在网络窗口中出现错误:
TypeError: Cannot read property 'toUpperCase' of undefined at dispatchXhrRequest (app.js:57825) at new Promise (<anonymous>) at xhrAdapter (app.js:57808) at dispatchRequest (app.js:58416)
这应该意味着一个承诺错误,也可能是一个配置错误,但我需要新的眼光来修复......
这是具有承诺的请求代码:
const headers = {
'X-CSRF-TOKEN': csrfToken,
'X-Requested-With': 'XMLHttpRequest',
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`,
};
client.interceptors.request.use(
config => {
if (!token) {
return config;
}
const newConfig = {
baseURL,
withCredentials: false,
headers: headers
};
return newConfig;
},
e => Promise.reject(e)
);
【问题讨论】:
标签: vue.js promise axios interceptor