为什么返回生成的 HTML 而不是 JSON 是一种不好的做法?或者是吗? [英] Why is it a bad practice to return generated HTML instead of JSON? Or is it?

查看:33
本文介绍了为什么返回生成的 HTML 而不是 JSON 是一种不好的做法?或者是吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 JQuery 或任何其他类似框架从您的自定义 URL/Web 服务加载 HTML 内容非常容易.我已经多次使用这种方法,直到现在,发现性能令人满意.

It is quite easy to load HTML content from your custom URLs/Web services using JQuery or any other similar framework. I've used this approach many times and till now and found the performance satisfactory.

但是所有的书,所有的专家都试图让我使用 JSON 而不是生成的 HTML.它如何比 HTML 优越得多?

But all the books, all the experts are trying to get me to use JSON instead of generated HTML. How's it much more superior than HTML?

是不是快了很多?
它对服务器的负载是否要小得多?

另一方面,我有一些使用生成的 HTML 的原因.

On the other side I have some reasons for using generated HTML.

  1. 它是简单的标记,通常与 JSON 一样紧凑或实际上更紧凑.
  2. 它不太容易出错,因为你得到的只是标记,没有代码.
  3. 在大多数情况下编程会更快,因为您不必为客户端单独编写代码.

你站在哪一边,为什么?

Which side are you on and why?

推荐答案

我有点两边,其实:

  • 当我在 javascript 方面需要的是数据时,我使用 JSON
  • 当我在 javascript 方面需要的是 presentation 时,我不会做任何计算,我通常使用 HTML
  • When what I need on the javascript side is data, I use JSON
  • When what I need on the javascript side is presentation on which I will not do any calculation, I generally use HTML

使用 HTML 的主要优点是当您想用 Ajax 请求返回的内容替换页面的整个部分时:

The main advantage of using HTML is when you want to replace a full portion of your page with what comes back from the Ajax request :

  • 在 JS 中重建页面的一部分(相当)困难
  • 您可能已经在服务器端使用了一些模板引擎,它首先用于生成页面...为什么不重用它?

我通常不会真正考虑事物的性能"方面,至少在服务器上:

I generally don't really take into consideration the "performance" side of things, at least on the server :

  • 在服务器上,生成一部分 HTML 或一些 JSON 可能不会有太大的不同
  • 关于通过网络传输的内容的大小:嗯,您可能不会使用数百 KB 的数据/html...在传输的任何内容上使用 gzip 会产生最大的不同 (不在 HTML 和 JSON 之间进行选择)
  • 不过,可以考虑的一件事是客户端需要哪些资源才能从 JSON 数据重新创建 HTML (或 DOM 结构)...比较一下将一部分 HTML 推送到页面中 ;-)
  • On the server, generating a portion of HTML or some JSON won't probably make that much of a difference
  • About the size of the stuff that goes through the network : well, you probably don't use hundreds of KB of data/html... Using gzip on whatever you are transferring is what's going to make the biggest difference (not choosing between HTML and JSON)
  • One thing that could be taken into consideration, though, is what resources you'll need on the client to recreate the HTML (or the DOM structure) from the JSON data... compare that to pushing a portion of HTML into the page ;-)

最后,一件很重要的事情:

Finally, one thing that definitly matters :

  • 开发一个以 JSON 格式发送数据 + JS 代码将数据以 HTML 格式注入页面所需的新系统需要多长时间?
  • 返回 HTML 需要多长时间?如果您可以重用一些现有的服务器端代码,需要多长时间?


并回答另一个答案:如果您需要更新页面的多个部分,仍然存在将所有这些部分发送到一个包含多个 HTML 部分的大字符串中的解决方案/技巧,并在 JS 中提取相关部分.


And to answer another answer : if you need to update more than one portion of the page, there is still the solution/hack of sending all those parts inside one big string that groups several HTML portions, and extract the relevant parts in JS.

例如,您可以返回一些如下所示的字符串:

For instance, you could return some string that looks like this :

<!-- MARKER_BEGIN_PART1 -->
here goes the html
code for part 1
<!-- MARKER_END_PART1 -->
<!-- MARKER_BEGIN_PART2 -->
here goes the html
code for part 2
<!-- MARKER_END_PART2 -->
<!-- MARKER_BEGIN_PART3 -->
here goes the json data
that will be used to build part 3
from the JS code
<!-- MARKER_END_PART3 -->

这看起来不太好,但它绝对有用(我已经使用它很多次了,主要是当 HTML 数据太大而无法封装到 JSON 中时):你正在为需要展示的页面部分发送 HTML,而您正在为需要数据的情况发送 JSON...

That doesn't look really good, but it's definitly useful (I've used it quite a couple of times, mostly when the HTML data were too big to be encapsulated into JSON) : you are sending HTML for the portions of the page that need presentation, and you are sending JSON for the situation you need data...

...为了提取这些,JS substring 方法可以解决问题,我想 ;-)

... And to extract those, the JS substring method will do the trick, I suppose ;-)

这篇关于为什么返回生成的 HTML 而不是 JSON 是一种不好的做法?或者是吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆