使用Node,Express& amp;将数据从一个页面传递到另一个页面EJS

时间:2017-08-14 17:53:07

标签: node.js express ejs

我已设置以下路线:

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

1 个答案:

答案 0 :(得分:1)

我认为你需要在<%- JSON.stringify(twitchLink) %>附近加上引号,如下所示:

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

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

var twitchLink = foo.bar.com

你想要的是:

var twitchLink = 'foo.bar.com'