我在express中有下一个GET函数,在res.render()
函数中,我要发送我想要使用的数据到EJS文件,现在我的问题是,我如何在相同的EJS文件中使用我以纯javascript格式发送到EJS文件的数据?获取函数:
router.get('/:id', (req, res) => {
let pollId = req.params.id;
Poll.findById(pollId, (err, poll) => {
if(err) throw err;
Vote.find(pollId, (err, votes) => {
res.render('vote', {user: req.user, poll: poll, voteFail: req.flash('voteFail'),votes: votes });
});
});
});
下面是我想要如何使用数据的示例:
<script type="text/javascript">
console.log(<%votes%>);
</script>
发布于 2018-06-14 04:16:49
有三个主要的EJS标签
<% %> and <%- %> and <%= %>
<% These will run script server side - There is no output - The user will not see this %>
<%- These will inject javascript value into your html, unsanitized - you can pass html this way. %>
<%= These tags will inject the javascript value into your html, sanitized - html tags will appear as strings %>
如果你想通过控制台登录浏览器控制台,你可以使用...
<script>
console.log( <%-votes%> );
</script>
如果您想通过console.log连接到服务器端控制台,只需使用...
<% console.log(votes); %>
发布于 2017-12-18 01:13:00
使用<% %>
标记不会计算表达式。请改用<%= votes %>
。
发布于 2018-06-10 04:02:46
尝试使用<%- votes -%>
,对我来说很管用。
https://stackoverflow.com/questions/47857392
复制相似问题