admin管理员组文章数量:1326326
I have on login form in login.ejs file when i click on submit after filling information i am redirecting to the page if detail are correct otherwise i want to show something in .ejs that password is wrong.Below are the detail
Here is my app.js file code- Here i am sending one json and hiding that invalid password in .ejs file
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'});
}
Here is my login.ejs file
<!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>
But I am unable to send json data in redirect.Is there any way i can do this?
I have on login form in login.ejs file when i click on submit after filling information i am redirecting to the page if detail are correct otherwise i want to show something in .ejs that password is wrong.Below are the detail
Here is my app.js file code- Here i am sending one json and hiding that invalid password in .ejs file
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'});
}
Here is my login.ejs file
<!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>
But I am unable to send json data in redirect.Is there any way i can do this?
Share Improve this question asked Jan 12, 2017 at 9:28 Nitesh SharmaNitesh Sharma 551 gold badge1 silver badge7 bronze badges 1-
Do you mean the
Error
is not being passed and displayed in yourlogin.ejs
? – David R Commented Jan 12, 2017 at 9:33
3 Answers
Reset to default 3According to express docs of .redirect method:
res.redirect([status,] path)
Redirects to the URL derived from the specified path, with specified status, a positive integer that corresponds to an HTTP status code . If not specified, status defaults to “302 “Found”.
So you can't call it like that:
res.redirect('/',{Error:'block'});
Even if that method work, it'll just redirect to '/'
, which will trigger the app.get('/', function (req, res) {...}
endpoint and show the initial login page, with error message hidden.
I believe, the best way it to change last line in app.post('/login', function (req, res) {...}
to
res.render("login",{Error:"block"});
You can't make a redirection after an AJAX call if you're using AJAX. Use send instead
res.send({Error:'block'});
You can pass json
with res.render
method but to do with res.redirect
you can use following.
- You can use
encodeURIComponent
app.get('/user', function(req, res) { var error = encodeURIComponent('error'); res.redirect('/?user=' + error); });
- Another way is using something in the session. Link to setup Sessions
req.session['error'] = "Error";
req.redirect('/');
- You can also use
express-flash
to flash any error on page Basic example an Usage of express-flash
本文标签: javascriptHow to pass Data from express to ejs file while redirecting in nodejsStack Overflow
版权声明:本文标题:javascript - How to pass Data from express to .ejs file while redirecting in node.js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742201630a2432068.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论