【问题标题】:Axios- redirect in issueAxios-重定向问题
【发布时间】:2021-03-28 12:31:19
【问题描述】:

在接受来自 Axios 的获取请求后,我无法让重定向正常工作。我确实知道请求正在发送,并且它至少从 URL 路由中获得了一些响应,

当我控制台 response.data.redirect 它返回未定义

const onSubmitHandler = (e) => {
    e.preventDefault()
    axios.get('/get/user')
        .then(function (response) {
            if (response.data.redirect == '/' || response.data.redirect == '/login' ) {
                window.location = "/login"
            } else {
              console.log(response.data)
             }
        })
        .catch(function(error) {
            window.location = "/login"
        })
}

我收到了状态代码 302,但它会自动重定向到页面而无需刷新页面,在 div 部分的同一页面上显示重定向页面。 任何人都知道我如何在 Axios 中使用拦截器,这样如果状态代码属于 200 系列,则执行其他页面刷新。我检查了 Axios 文档,但没有实现

【问题讨论】:

    标签: axios


    【解决方案1】:

    拦截器试试这个

    axios.interceptors.response.use(
        response => {
            return response.data;
        },
        err => {
            //return new Promise((resolve, reject) => {
                //err.response.status for getting error status
                // }
                throw err;
            });
        }
    );
    

    【讨论】:

      猜你喜欢
      • 2011-02-04
      • 2020-04-15
      • 2021-03-30
      • 2011-11-18
      • 2016-05-21
      • 2017-03-20
      • 2012-02-13
      • 2011-03-08
      相关资源
      最近更新 更多