【问题标题】:How to add unescaped stringified JSON in EJS?如何在 EJS 中添加未转义的字符串化 JSON?
【发布时间】:2021-03-22 21:03:37
【问题描述】:

我正在尝试在 EJS 模板中使用 JavaScript 发送postMessage,我需要发送一个简单的字符串化 JSON 对象,如下所示:

{ "id": "1234567890" }

当我尝试在 EJS 模板中传递此对象时,双引号 (") 被转义为 "。这是我的模板:

<script>
    if (window.opener) {
        window.opener.postMessage('<%= JSON.stringify(user) %>', "http://localhost:3000")
        window.close()
    }
</script>

字符串化的对象变成了这个:

&#34;{&#34;id&#34;:&#34;1234567890&#34;}&#34;

如何防止特殊字符被 EJS 转义?

【问题讨论】:

    标签: javascript node.js express ejs


    【解决方案1】:

    原来我需要将&lt;%= 替换为&lt;%- 才能使用非转义字符。

    所以我的代码必须是:

    <script>
        if (window.opener) {
            window.opener.postMessage('<%- JSON.stringify(user) %>', "http://localhost:3000")
            window.close()
        }
    </script>
    

    来源:https://github.com/tj/ejs/tree/1.0.0#features

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-29
      • 1970-01-01
      • 2011-09-17
      • 1970-01-01
      • 2016-06-26
      • 2017-01-02
      相关资源
      最近更新 更多