问题
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");
});
I checked and app.get('/auth') gets called but it seems the second redirect to instagram doesn't do anything.
回答1:
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);
回答2:
Are you using Ajax ? If yes, you can't directly redirect, but send back the URL, and the front end will redirect to the URL your server sent.
来源:https://stackoverflow.com/questions/35182383/res-redirect-doesnt-redirect-node-express-js