如何使用Axios的“获取”请求实际重定向到Express / Node中设置的页面?

我对为什么使用axios进行get请求时为何无法重定向到我在node / express中设置的视图感到困惑?我希望它的行为就像在浏览器的地址栏中键入/ adminPortal一样吗?我需要这样做的原因是因为AXIOS已在每个get请求中添加了用户的身份验证令牌。例如,当用户转到/ adminPortal时,服务器端将检查以确保请求标头中的“授权”元素是管理员用户的元素。当我console.log响应时,它只是一堆html,我实际上希望页面像往常一样重定向到响应吗?这是在客户端上附加用户的身份验证令牌的代码:axios.defaults.headers.common['authorization'] = idToken; 这是向页面(/ adminPortal索引)发出请求的客户端代码。

  axios({
                url: '/adminPortal',method: 'get'
              })
            axios.get('/adminPortal',{

            // })
            .then(function (response) {

                response.render();
            })
            .catch(function (error) {
                console.log(error);
            })
            .finally(function () {
                // always executed
            }); 

这是服务器端代码MIDDLEWARE,它在每次请求/ adminPortal GET时运行,以检查这样做的授权。满足此条件后,它将返回页面,浏览器将重定向到该页面,但不会:(我将Firebase用作后端)


    //Check if request is from User account
    isLoggedInAsUser: async function(req,res,next){

            req.header('User-Agent')
            const idToken = req.headers.authorization;
            console.log("idToken:"+idToken);
            console.log("TEST"+ JSON.stringify(req.headers));

            try {
                const decodedToken = await admin.auth().verifyIdToken(idToken);
                if(decodedToken){
                    req.body.uid = decodedToken.uid;
                    return next();
                } else{
                    return res.status(401).send("You are not autheeeroized");
                }
            } catch (e){
                return res.status(401).send("You are not authroized" + e);
            }

    } 


xxd1111 回答:如何使用Axios的“获取”请求实际重定向到Express / Node中设置的页面?

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3128481.html

大家都在问