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

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

问题描述

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

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

是不是非常快?
它有一个非常小的负载在服务器上?

在另一边,我有一些理由使用生成的HTML。

  1. 这是简单的标记,而且往往只是作为紧凑型或实际上比JSON更紧凑。
  2. 这是不容易出错原因所有你要的标记,并没有code。
  3. 这会更快,在大多数情况下,编程,因为你不会写code分别为客户端。

哪一方是你,为什么?

解决方案

我有点两侧,实际上:

  • 当什么,我需要在JavaScript端的数据,我用JSON
  • 当什么,我需要在JavaScript端的 presentation 上,我不会做任何的计算,我一般使用HTML

当你想替换您的网页什么回来从Ajax请求的完整部分使用HTML的主要优点是:

  • 重新建设JS网页的一部分(相当)硬
  • 您可能已经在服务器端,这是用来产生在首位的页面有些模板引擎......为什么不重用呢?

我一般不真正考虑到事物的表演的一面,至少在服务器上的:

  • 在服务器上,生成HTML或一些JSON的一部分很可能不会作出太大的差别
  • 关于东西的尺寸经过网络:还有,你可能不使用几百KB的数据/ HTML ...上无论你是转移是怎么回事,使最大的区别使用gzip的(HTML和JSON之间不选择)
  • 在可能被考虑的一件事,虽然是你需要在客户端上的哪些资源,从JSON数据重新创建HTML的(或DOM结构)的......比较一下到推的HTML的一部分插入的页面; - )

最后,有一点definitly事项:

  • 在它需要多长时间,你开发一个新的系统,该系统将发送数据作为JSON + code中的JS需要将其作为注入到HTML页面?
  • 需要多长时间才能只返回HTML吗?多久,如果你可以重新使用您的一些现有的服务器端code?


而回答另一个答案:如果您需要更新页面的多个部分,还有发送在一个大的字符串,集团多个HTML部分的所有部分的解决方案/黑客,并提取相关部分的JS <。 / P>

例如,你可以返回一些字符串看起来是这样的:

 &LT;! -  MARKER_BEGIN_PART1  - &GT;
这里有云的HTML
code为第1部分
&所述;! -  MARKER_END_PART1  - &GT;
&所述;! -  MARKER_BEGIN_PART2  - &GT;
这里有云的HTML
code为第2部分
&所述;! -  MARKER_END_PART2  - &GT;
&所述;! -  MARKER_BEGIN_PART3  - &GT;
在这里不用JSON数据
将用于建立部3
从JS code
&所述;! -  MARKER_END_PART3  - &GT;
 

这看起来真的很不错,但它的 definitly有用的(我用它颇有几次,大多是当HTML数据都太大而被封装成JSON)的:你发送的HTML需要presentation页面的部分,和您发送的JSON你需要数据的情况......

...并提取这些,JS的子方法也有问题,我想, - )

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.

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?

Is it very much faster?
Does it have a very much lesser load on the server?

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

  1. It's simple markup, and often just as compact or actually more compact than JSON.
  2. It's less error prone cause all you're getting is markup, and no code.
  3. It will be faster to program in most cases cause you won't have to write code separately for the client end.

Which side are you on and why?

解决方案

I'm a bit on both sides, actually :

  • 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

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 :

  • Re-building a portion of page in JS is (quite) hard
  • You probably already have some templating engine on the server side, that was used to generate the page in the first place... Why not reuse it ?

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

  • 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 :

  • How long will it take you to develop a new system that will send data as JSON + code the JS required to inject it as HTML into the page ?
  • How long will it take to just return HTML ? And how long if you can re-use some of your already existing server-side code ?


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 -->

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...

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

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

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