在 DOM 中嵌入任意 JSON 的最佳实践?
我正在考虑像这样在 DOM 中嵌入任意 JSON:
<script type="application/json" id="stuff">
{
"unicorns": "awesome",
"abc": [1, 2, 3]
}
</script>
这类似于在 DOM 中存储任意 HTML 模板以供以后与 JavaScript 模板引擎一起使用的方式。在这种情况下,我们稍后可以检索 JSON 并使用以下方法解析它:
var stuff = JSON.parse(document.getElementById('stuff').innerHTML);
这可行,但这是最好的方法吗?这是否违反任何最佳实践或标准?
注意:我并不是在寻找在 DOM 中存储 JSON 的替代方案,我已经确定这是针对我遇到的特定问题的最佳解决方案。我只是在寻找最好的方法。
I'm thinking about embedding arbitrary JSON in the DOM like this:
<script type="application/json" id="stuff">
{
"unicorns": "awesome",
"abc": [1, 2, 3]
}
</script>
This is similar to the way one might store an arbitrary HTML template in the DOM for later use with a JavaScript template engine. In this case, we could later retrieve the JSON and parse it with:
var stuff = JSON.parse(document.getElementById('stuff').innerHTML);
This works, but is it the best way? Does this violate any best practice or standard?
Note: I'm not looking for alternatives to storing JSON in the DOM, I've already decided that's the best solution for the particular problem I'm having. I'm just looking for the best way to do it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我认为你原来的方法是最好的。 HTML5 规范甚至解决了这种用法:
阅读此处: http://dev .w3.org/html5/spec/Overview.html#the-script-element
您已经这样做了,这有什么不值得喜欢的?如果您可以对属性数据进行格式化,则可以。它具有表现力,并且预期用途很明确。感觉不像是黑客(例如使用 CSS 来隐藏“载体”元素),这是完全有效的。
I think your original method is the best. The HTML5 spec even addresses this use:
Read here: http://dev.w3.org/html5/spec/Overview.html#the-script-element
You've done exactly that. What is not to love? No character encoding as needed with attribute data. You can format it if you want. It's expressive and the intended use is clear. It doesn't feel like a hack (e.g. as using CSS to hide your "carrier" element does). It's perfectly valid.
作为一般方向,我会尝试使用 HTML5 数据属性。没有什么可以阻止您输入有效的 JSON。例如:
如果您使用 jQuery,那么检索它就像以下一样简单:
As a general direction, I would try using HTML5 data attributes instead. There's nothing to stop you putting in valid JSON. e.g.:
If you're using jQuery, then retrieving it is as easy as:
这种在脚本标签中嵌入 json 的方法存在潜在的安全问题。假设 json 数据源自用户输入,则可以制作一个数据成员,该数据成员实际上会突破脚本标记并允许直接注入到 dom 中。请参阅此处:
http://jsfiddle.net/YmhZv/1/
这是注入,
没有办法逃脱/编码。
This method of embedding json in a script tag has a potential security issue. Assuming the json data originated from user input, it is possible to craft a data member that will in effect break out of the script tag and allow direct injection into the dom. See here:
http://jsfiddle.net/YmhZv/1/
Here is the injection
There is just no way around escaping/encoding.
HTML5 包含
元素 用于保存机器可读的数据。作为
的替代方案(也许更安全),您可以将 JSON 数据包含在该元素的
value
属性中。在这种情况下,如果您选择用双引号将值括起来,则需要将所有单引号替换为
'
或"
。否则,您将面临 XSS 攻击的风险,就像其他答案所建议的那样。HTML5 includes a
<data>
element for keeping machine readable data. As a—perhaps safer—alternative to<script type="application/json">
you could include your JSON data inside thevalue
attribute of that element.In this case you need to replace all single quotes with
'
or with"
if you opt to enclose the value with double quotes. Otherwise your risk XSS attacks like other answers have suggested.请参阅 OWASP 的 XSS 预防备忘单中的规则#3.1。
假设您想在 HTML 中包含此 JSON:
在 HTML 中创建一个隐藏的
。接下来,通过编码不安全实体(例如,&、<、>、"、'、和、/)来转义 JSON,并将其放入元素内。
现在您可以通过读取
textContent 并解析它:
See Rule #3.1 in OWASP's XSS prevention cheat sheet.
Say you want to include this JSON in HTML:
Create a hidden
<div>
in HTML. Next, escape your JSON by encoding unsafe entities (e.g., &, <, >, ", ', and, /) and put it inside the element.Now you can access it by reading the
textContent
of the element using JavaScript and parsing it:我建议将 JSON 放入带有函数回调的内联脚本中(类似于 JSONP):
如果执行脚本是在文档之后加载,您可以将其存储在某处,可能带有附加标识符参数:
someCallback("stuff", { ... });
I'd suggest to put JSON into an inline script with a function callback (kind of JSONP):
If the executing script is loaded after the document you can store this somewhere, possibly with an additional identifier argument:
someCallback("stuff", { ... });
我的建议是将 JSON 数据保存在外部
.json
文件中,然后通过 Ajax 检索这些文件。您不会将 CSS 和 JavaScript 代码放入网页(内联),那么为什么要使用 JSON 呢?My recommendation would be to keep JSON data in external
.json
files, and then retrieve those files via Ajax. You don't put CSS and JavaScript code onto the web-page (inline), so why would you do it with JSON?