简体   繁体   English

在node.js中重定向时如何将数据从express传递到.ejs文件

[英]How to pass Data from express to .ejs file while redirecting in node.js

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当我在填写信息后单击提交时,我在 login.ejs 文件中有登录表单,如果详细信息正确,我将重定向到该页面,否则我想在 .ejs 中显示密码错误的内容。以下是详细信息

Here is my app.js file code- Here i am sending one json and hiding that invalid password in .ejs file这是我的 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'});
}

Here is my login.ejs file这是我的 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>

But I am unable to send json data in redirect.Is there any way i can do this?但是我无法在重定向中发送 json 数据。有什么办法可以做到这一点?

According to express docs of .redirect method:根据 .redirect 方法的 express文档

res.redirect([status,] path) res.redirect([状态,] 路径)

Redirects to the URL derived from the specified path, with specified status, a positive integer that corresponds to an HTTP status code .重定向到从指定路径派生的 URL,具有指定状态,对应于 HTTP 状态代码的正整数。 If not specified, status defaults to “302 “Found”.如果未指定,则状态默认为“302“找到”。

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.即使该方法有效,它也会重定向到'/' ,这将触发app.get('/', function (req, res) {...}端点并显示初始登录页面,并带有错误消息隐。

I believe, the best way it to change last line in app.post('/login', function (req, res) {...} to我相信,最好的方法是将app.post('/login', function (req, res) {...}最后一行更改为

res.render("login",{Error:"block"});

You can't make a redirection after an AJAX call if you're using AJAX.如果您使用 AJAX,则无法在 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.您可以使用res.render方法传递json ,但要使用res.redirect您可以使用以下方法。

  1. You can use encodeURIComponent您可以使用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您还可以使用express-flash来刷新页面上的任何错误基本示例以及 express-flash 的用法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM