[英]Javascript accessing ejs variable in Node.js context
I am trying my javascript code inside an HTML file to access an ejs variable. 我正在尝试将HTML文件中的javascript代码访问ejs变量。
This is the Node.js code: 这是Node.js代码:
res.render('target.ejs', {data:user});
When i do this in HTML: 当我在HTML中执行此操作时:
<p> <h1><%= data %></h1> </p>
evertything is ok. 一切都还可以。
But when I try to access the variable inside a <script>
, like this: 但是,当我尝试访问
<script>
内的变量时,如下所示:
<script>
var name = <%= data %>;
alert(name);
</script>
then nothing happens. 那什么也没发生 I also tried with a
console.log(name);
我也尝试了
console.log(name);
and still it didn't work. 仍然没有用。
Seems like you need some quotes. 好像您需要一些引号。
Try 尝试
var name = '<%= data %>';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.