res.redirect('back') with parameters

后端 未结 7 2016
刺人心
刺人心 2020-12-23 09:44

I have a register form on every page of my website. During registration some error may occur. After catching the error, I have to return the user to the previous page, sho

7条回答
  •  情书的邮戳
    2020-12-23 10:25

    You could simply have it redirect as res.redirect('..?error=1')

    the ? tag tells the browser that it is a set of optional parameters and the .. is just a pathname relative recall (like calling cd .. on terminal to move back one directory) and your browser will direct to the appropriate page with that tag at the end: http://.....?error=1

    then you can simply pull the error on the appropriate page by doing a:

    if (req.param("error" == 1)) { 
    // do stuff bassed off that error match
    };
    

    you can hardcode in several different error values and have it respond appropriately depending on what error occurred

    Note that this makes your webpage susceptible to manipulation since users can type in their own parameters and get your page to respond to it, so make sure the action you take is not something you wouldn't want people abusing. (Should be fine for simple error displaying)

提交回复
热议问题