最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to pass Data from express to .ejs file while redirecting in node.js - Stack Overflow

programmeradmin2浏览0评论

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 your login.ejs ? – David R Commented Jan 12, 2017 at 9:33
Add a ment  | 

3 Answers 3

Reset to default 3

According 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.

  1. You can use encodeURIComponent
    app.get('/user', function(req, res) { var error = encodeURIComponent('error'); res.redirect('/?user=' + error); });
  2. Another way is using something in the session. Link to setup Sessions
    req.session['error'] = "Error";
    req.redirect('/');
  3. You can also use express-flash to flash any error on page Basic example an Usage of express-flash
发布评论

评论列表(0)

  1. 暂无评论