search

Home  >  Q&A  >  body text

node.js - nodejs redirect failure problem

1. After sending the superagent request, process the data in the background, and then choose whether to redirect based on the results but it always fails.
2. The code is as follows:

//superagent请求
 request.get(`/allStudios`)
                .end((err,res) =>{
                    next({type:'ALL_STUDIOS',content:res.body})
                });
//后台处理
router.get('/allStudios',(req,res,next)=>{
    Studios.find({},(err,data)=>{
        if(req.headers.cookie){
            res.send(data);
        }
        else {
            res.redirect(302,'http://localhost:3000');
        }
    })
});

3. The result is that there is no way to implement redirection

Please help me solve it, thank you!

PHPzPHPz2799 days ago1345

reply all(1)I'll reply

  • 给我你的怀抱

    给我你的怀抱2017-06-06 09:55:14

    ajax cannot be redirected, but you can make your own judgment after getting the data at the front end. If you need to redirect, use window.location.href = 'new address' to jump.

    reply
    0
  • Cancelreply