【发布时间】:2021-05-07 05:31:41
【问题描述】:
如果我收到任何 404 错误,则在 catch 块中记录错误具有该状态代码,但如果我从服务器收到 504 错误,则在 catch 中记录 error.response 是未定义的。发生这种情况的任何原因?
try {
const response = await axios.request({
method,
url,
headers: {
'Content-Type': 'application/json;charset=UTF-8',
},
...config,
});
return response;
} catch (error) {
console.dir(error);
if (error.response && error.response.status === 401) {
const isNonAuthUrl = window.location.pathname.match(/\/verify\//);
if (!isNonAuthUrl) {
localStorage.clear();
window.location.reload();
} else {
throw error;
}
} else {
throw error;
}
}
【问题讨论】:
-
我在这里转载了你的问题~jsfiddle.net/vjf85e94。如果我用 curl 打
https://httpstat.us/504/cors,它似乎工作得很好。但是,当请求来自浏览器(使用axios或fetch)时,由于 CORS 错误,它会失败。同样的事情发生在 502
标签: javascript axios