res.redirect不会重定向Node/Express.js [英] res.redirect doesn't redirect Node/Express.js

查看:231
本文介绍了res.redirect不会重定向Node/Express.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的Web应用程序使用Express,并且正在检查会话中是否不存在某些值,请将用户重定向到instagram登录页面.但是在控制台中,我一直收到302代码,浏览器没有重定向用户,这是我的代码.我正在使用Ajax发送请求.

I'm using Express for my webapp and I'm checking to see if some value doesn't exists in session, redirect the user to instagram login page. But in the console I keep getting 302 code and the browser doesn't redirect the user here is my code. I'm sending my requests using Ajax.

    function requireLogin (req, res, next) {
     if (!req.token) 
        res.redirect('/auth');
     else 
        next();      
    };

 app.get('/auth',function(req,res,next){
   res.redirect("https://api.instagram.com/oauth/authorize/?client_id="+client_id+"&redirect_uri="+redirect_uri+"&score=public_content+likes+comments+follower_list+relationships&response_type=code");
});

我检查了一下并调用了app.get('/auth'),但似乎第二次重定向到instagram并没有任何作用.

I checked and app.get('/auth') gets called but it seems the second redirect to instagram doesn't do anything.

推荐答案

问题是,如果您使用的是Ajax,则res.redirect不会重定向用户.所以我将网址传递给客户端,并使用了window.location.replace(url);

The problem was that res.redirect does not redirect the user if you are using Ajax. So I passed the url to client side and used window.location.replace(url);

这篇关于res.redirect不会重定向Node/Express.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆