JavaScript: 如何将JSON格式化为HTML来发送邮件?

JavaScript: How to format JSON to HTML to send email?

在我的应用程序中,任何错误都会被捕获并通过电子邮件发送以供审核。我目前正在向电子邮件添加 headers 和 body 以帮助重现错误。它目前显示在一行中。我想以格式化的方式显示此行,以便更容易阅读电子邮件。

我目前正在使用 JSON.stringify(myJsonHere, null, 2) 格式化我的 JSON。如果我使用 console.log(JSON.stringify(myJsonHere, null, 2)) 显示它,它确实有效,但当我将它添加到 HTML 电子邮件(如 '<b>Headers:</b> ' JSON.stringify(myJsonHere, null, 2).

时不起作用

我的目标是获得这种格式:

{
  data: "test",
  id: 1,
  user: {
    id: 1,
    user: "test"
  }
}

而不是这种格式:

{data: "test", id: 1, user: {id: 1, user: "test"}}

const pre = document.querySelector('pre');

pre.innerHTML = JSON.stringify({ test: 1, nested: { a: 1} }, null, 2)
<pre></pre>