我在 login.ejs 文件中有登录表单,当我填写信息后单击“提交”时,如果详细信息正确,我将重定向到该页面,否则我想在 .ejs 中显示密码错误的内容。以下是详细信息
这是我的 app.js 文件代码 - 这里我发送一个 json 并将该无效密码隐藏在 .ejs 文件中
app.get('/', function (req, res) {
res.render("login",{Error:"none"});
});
app.post('/login', function (req, res) {
var username = req.body.username;
var password = req.body.password;
//Here will be my code to check detail wrong or right
res.redirect('/',{Error:'block'});
}
这是我的login.ejs 文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title></title>
<link rel="stylesheet" href="css/style.css" type="text/css" />
<link rel="stylesheet" href="css/font.css" type="text/css" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div class="Error" id="error" style="display:<%=Error%>;">Invalid email/phone or password</div>
<div class="Body">
<form class="LogBox" action="/login" method="post">
<div class="Head">Log In</div>
<input type="text" placeholder="Email or Phone" class="inputfrontbutton" name="username">
<input type="password" placeholder="Password" class="inputfrontbutton" name="password">
<input type="submit" value="Log In" class="frontsubmit">
</form>
</div>
</body>
</html>
但我无法在重定向中发送 json 数据。有什么办法可以做到这一点吗?
据快报docs.redirect 方法的:
res.redirect([状态,]路径)
重定向到从指定路径派生的 URL,具有指定状态(对应于 HTTP 状态代码 的正整数)。如果未指定,状态默认为“302“Found”。
所以你不能这样称呼它:
res.redirect('/',{Error:'block'});
即使该方法有效,它也只会重定向到'/'
,这将触发app.get('/', function (req, res) {...}
端点并显示初始登录页面,并隐藏错误消息。
我相信,改变最后一行的最好方法app.post('/login', function (req, res) {...}
to
res.render("login",{Error:"block"});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)