可以通过设置axios的'maxRedirects'配置选项来处理重定向。将其设置为0将禁用重定向,将其设置为1将允许重定向,以此类推。以下是一个例子:
axios.get('https://example.com', {
maxRedirects: 5 // 允许跟随最多5个重定向
}).then(response => {
console.log(response);
}).catch(error => {
console.log(error);
});
如果您希望只处理特定类型或状态码的重定向,请使用interceptor来拦截响应。以下是一个例子:
axios.interceptors.response.use(response => {
if (response.status === 301 || response.status === 302) {
// 处理重定向逻辑
}
return response;
});