reactjs - 如果我使用 axios 收到 404 错误,如何重定向?
问题描述
如果我收到 404 错误,除了一个路由 - /api/profile 之外,我想重定向。
我使用 axios,我不确定是否可以在这种情况下使用拦截器。
我可以做这样的事情吗?
axios.interceptors.response.use( ( response ) => {
return response;
}, ( error ) => {
if ( error.response.status === 404 && error.config.url !== '/api/profile' ) {
history.push( '/not-found' );
return Promise.reject( error );
}
return Promise.reject( error );
});
还是我应该坚持这一点:
axios.get( '/api/something' ).then( ( res ) => {
dispatch({
type: GET_SOMETHING,
payload: res.data
});
}).catch( ( err ) => {
if ( err.response.status == 404 ) {
history.push( '/not-found' );
}
});
谢谢
解决方案
推荐阅读
- c - 为什么尚未到达文件末尾时会收到 EOF?
- javascript - 动态创建哈希哈希?
- django - 考虑到多对多关系的一个元素,如何对模型列表进行排序?
- php - (失败)net::ERR_CONNECTION_RESET 上大文件上传 php
- javascript - 如何在 Jest 的 xyz.test.js 中导入“.mjs”模块?
- java - JUNITS:MockRestServiceServer 未使用 Maven 构建运行
- java - 为可执行 JAR 文件提供外部依赖项
- android - 当我想使用 Android Studio 生成应用程序包时出错:需要更新 Gradle
- python - 根据 Pandas 中其他列中的值计算列值
- c++ - llvm IR 中的内联函数