【问题标题】:Passing data from one page to another using Node, Express & EJS使用 Node、Express 和 EJS 将数据从一个页面传递到另一个页面
【发布时间】:2017-08-14 17:53:07
【问题描述】:

我设置了以下路线:

router.get('/', function(req, res) {
    res.render('index', {});
});

router.post('/application', function(req, res) {
  res.render('application', {twitchLink : req.query.twitchLink});
});

我已经正确设置了两个视图。

这是我在“索引”视图中看到的:

<form class="form-horizontal" action="/application", method="post", role="form">
    <input type="url" name="twitchLink" required>
    <button class="btn btn-success">Submit</button>
</form>

提交此表单确实会将我带到应用程序视图。

<script>var twitchLink = <%- JSON.stringify(twitchLink) %></script>
<script>console.log(twitchLink)</script>

这应该注销我提交的链接,对吧? 但是,我得到了这两行:

Uncaught SyntaxError: Unexpected end of input
Uncaught ReferenceError: twitchLink is not defined

【问题讨论】:

  • 你找到解决办法了吗?

标签: node.js express ejs


【解决方案1】:

我认为您需要在&lt;%- JSON.stringify(twitchLink) %&gt; 周围加上引号,如下所示:

var twitchLink = '<%- JSON.stringify(twitchLink) %>'

在您的示例中,它将显示为:

var twitchLink = foo.bar.com

你想要的是:

var twitchLink = 'foo.bar.com'

【讨论】:

    猜你喜欢
    • 2020-11-20
    • 2012-01-03
    • 1970-01-01
    • 2021-04-22
    • 1970-01-01
    • 1970-01-01
    • 2013-02-03
    • 2017-04-08
    • 1970-01-01
    相关资源
    最近更新 更多