Reactjs Axios拦截器不会在React应用程序中重定向浏览器
我有一个带有OAuth2代理的Redux/React应用程序。当代理给出401响应时,需要将用户重定向到身份验证页面。下面是使用axios和拦截器的基本思想Reactjs Axios拦截器不会在React应用程序中重定向浏览器,reactjs,redux,react-router,axios,Reactjs,Redux,React Router,Axios,我有一个带有OAuth2代理的Redux/React应用程序。当代理给出401响应时,需要将用户重定向到身份验证页面。下面是使用axios和拦截器的基本思想 import axios from 'axios' export const axiosApi = axios.create({ baseURL: '/proxy/api', }) axiosApi.interceptors.response.use(response => { // Intercept successfu
import axios from 'axios'
export const axiosApi = axios.create({
baseURL: '/proxy/api',
})
axiosApi.interceptors.response.use(response => {
// Intercept successful response data
return response
}, error => {
if (error.response && error.response.status === 401) {
// Redirect when not authenticated
window.location.href = '/proxy/authorize'
}
return Promise.reject(error);
})
这就是使用axiosApi的Redux操作的样子:
export const fetchUsers = () => dispatch =>
new Promise((resolve, reject) => {
axiosApi.get(`users`)
.then(res => {
dispatch(fetchUsersData(res.data))
resolve(res)
})
.catch(err => {
reject(err)
})
})
问题是
window.location.href='/proxy/authorize'
实际上并没有重定向浏览器。这是因为使用了react路由器(V5)?window.location.replace我尝试了window.location.replace('proxy/authorize')
,但没有任何区别。您需要完整的url和http/httpst,这似乎不是问题所在。为什么我需要完整的url呢?尝试打开一个控制台并进行定位。替换('/users/4038702/pandelis')