简体   繁体   English

通过JSON接收HTML的最佳做法

[英]Best practices to receive html via json

I'm trying to retrieve an HTML via JSON and i don't know if i'm implementing the best practice for that... 我正在尝试通过JSON检索HTML,但我不知道我是否正在为此实现最佳实践...

Here's an example: 这是一个例子:

jsonRequest = [
{
  "id": "123",
  "template": '<div class=\"container\">Hello, guys, i am a json</div>'
}];

There's some other way to use it? 还有其他使用方式吗?

Not sure if there is really a "best practice" in that case. 不知道在这种情况下是否真的存在“最佳实践”。 This depends on multiple factors. 这取决于多个因素。

First JSON is just a notation, so as long as you provide a valid String content for the template property it is valid (and best practice) 第一个JSON只是一种表示法,因此,只要您为模板属性提供有效的String内容,它就是有效的(以及最佳做法)

Second I would treat it differently based on the template size. 其次,我将根据模板大小来区别对待。 If it is only such a small html template, I think wrapping it inside a JSON can make sense in some cases. 如果只是这么小的html模板,我认为在某些情况下将其包装在JSON中是有意义的。

However if you try to render an entire page with the template, I'd suggest to only provide a link to the template in the property and then fetch the template with this link. 但是,如果您尝试使用模板呈现整个页面,则建议仅在属性中提供指向模板的链接,然后使用此链接获取模板。 This keeps your JSON clean and readable (and a lot easier to debug) 这样可以使您的JSON整洁易读(并且易于调试)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM