I want to transfer data from my node.js (express) app to a variable in the <script>
on the page.
On the server-side I have
let tmp = JSON.stringify(await f(i));
console.log(tmp); //correct data [{"id":"729199920929701988","a ...}]
res.render('r.ejs', {"guilds": tmp});
Then in r.ejs
I have
<input type='hidden' id='getter' value="<%= guilds %>">
<script>
console.log(document.getElementById("getter").value); //correct value: [{"id":"729199920929701988","...}]
let v = "<%= guilds %>";
console.log(v);
console.log("<%= guilds %>"); //dumb value with " changed to special symbols: [{"id":"729199920929701988","...}]
</script>
The way with making an extra tag does not look like a good way in terms of performance. At least it makes the browser create one more element which takes time, especially if more than one element like this will be needed.
I would like to get the data directly in the <script>
tag. There should be a way to avoid changing "
to "
when getting the value directly in the <script>
tag (in fact, I do not understand why does that even happen). Can anyone help with that?
Just use let v = <%- guilds %>;
instead of let v ="<%= guilds %>";
to tell ejs you need unescaped data
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.