简体   繁体   English

对于 AJAX 响应,JSON 优于 HTML 的优势?

[英]Advantage(s) of JSON over HTML for AJAX response?

I was reading the code and JS used in Google websites via firebug.我正在通过 firebug 阅读 Google 网站中使用的代码和 JS。

In the Google Music website when we click on left navigation links then Google loads the songs in right body via ajax.在 Google 音乐网站中,当我们点击左侧导航链接时,Google 会通过 ajax 将歌曲加载到右侧。

Now When i want to load the content via Ajax then i normally get the HTML from get method and replace the HTML of body with new HTML received现在,当我想通过 Ajax 加载内容时,我通常会从 get 方法中获取 HTML 并用新收到的 HTML 替换 body

But in Google music i see that when i click on nav links then Google gets the JSON data of 1000s of songs with all title, album and then build the html on fly.但在谷歌音乐中,我看到当我点击导航链接时,谷歌会获取 1000 首歌曲的 JSON 数据,其中包含所有标题、专辑,然后即时构建 html。 if i had to do that same thing i would have called the page get the full page HTML and then replace the body如果我必须做同样的事情,我会调用页面获取整页 HTML 然后替换正文

So i want to know what is the advantage of using JSON the way Google did it所以我想知道像谷歌那样使用 JSON 有什么好处

JSON will likely be a significantly smaller data response than HTML and so will download to the client faster. JSON 的数据响应可能比 HTML 小得多,因此下载到客户端的速度更快。 However, the browser you are using will strongly affect how quickly the HTML is constructed on the client and loaded into the DOM (with older versions of IE being the slowest).但是,您使用的浏览器将极大地影响 HTML 在客户端上构建并加载到 DOM 中的速度(旧版本的 IE 最慢)。 It would be interesting to see if the page behaved differently for browsers with slower javascript engines.看看页面对于具有较慢 javascript 引擎的浏览器的行为是否有所不同会很有趣。

In my own testing I have seen IE 6 take 60x longer than Chrome for building an HTML table from JSON with 150 rows and 5 columns.在我自己的测试中,我发现 IE 6 从 JSON 构建具有 150 行和 5 列的 HTML 表所需的时间比 Chrome 长 60 倍。

HTML is a way to control the way that data looks. HTML 是一种控制数据外观的方法。 When you write an HTML tag like this:当您像这样编写 HTML 标签时:

<h1>Your Title</h1>

The text "Your Title" is the data.文本“Your Title”是数据。 The h1 tag is the presentation. h1 标签是演示文稿。 Most seasoned developers try to separate these two things (data and presentation) as much as possible.大多数经验丰富的开发人员都试图尽可能地将这两件事(数据和表示)分开。 The philosophy behind this separation is simple: if the data is always just the data, then you can change the way this data is presented a lot easier.这种分离背后的理念很简单:如果数据始终只是数据,那么您可以更轻松地更改这些数据的呈现方式。 Say Google wants to refresh the way Google Music looks, (which I hope they do soon) then they won't have to touch the data model or the way any of their ajax calls work, the data is still just the Artist name, track title, etc.假设谷歌想要刷新谷歌音乐的外观(我希望他们很快就会这样做)那么他们就不必触摸数据 model 或他们的任何 ajax 调用工作的方式,数据仍然只是艺术家姓名,曲目标题等

On the other hand if they were sending data and presentation together, then they would have to revamp everything -- Maybe the new look and feel doesnt call for the artist title to be in an h1 tag, but the AJAX call returns <h1>Artist title</h1> then they have to change the way the data is stored, the way the Ajax call returns the data, instead of just populating a new tag with the artist's name.另一方面,如果他们一起发送数据和演示文稿,那么他们将不得不修改所有内容——也许新的外观和感觉并不要求艺术家标题位于 h1 标签中,但 AJAX 调用返回<h1>Artist title</h1>然后他们必须改变数据的存储方式,Ajax 调用返回数据的方式,而不是仅仅用艺术家的名字填充一个新标签。

This is a core fundamental principle in design patterns and almost every design pattern that exists follows this principle.这是设计模式中的核心基本原则,几乎所有存在的设计模式都遵循这一原则。 If you have ever heard of mvc thats what it is about, separation of your layers.如果您听说过mvc ,那就是它的含义,即分层。 Model represents the data, view represents the markup or the presentation, and controller represents the logic which controls how these two interact. Model 代表数据,视图代表标记或表示,controller 代表控制这两者如何交互的逻辑。

That's why handling JSON in your data calls will help you to not run into problems later on and your code will be cleaner and simpler as JSON is a very simple data format.这就是为什么在您的数据调用中处理 JSON 将帮助您以后不会遇到问题,并且您的代码将更简洁,因为 JSON 是一种非常简单的数据格式。 (also as @alex-gitelman already said, it is faster to transfer!) (也正如@alex-gitelman 已经说过的那样,转移速度更快!)

It defers the layout and presentation of the data to the page that calls the Ajax method.它将数据的布局和呈现推迟到调用 Ajax 方法的页面。 in your case you format the HTML on the server.在您的情况下,您在服务器上格式化 HTML 。

JSON is just a way to represent data, while html represents data+markup. JSON 只是一种表示数据的方式,而 html 表示数据+标记。 So html will be lot bigger in size as it includes more info.所以 html 的尺寸会更大,因为它包含更多信息。 Also if html is used, that means server does, in effect, all rendering for the page but what if you want to populate parts of data in different frames?此外,如果使用 html,这意味着服务器实际上会为页面进行所有渲染,但是如果您想在不同的帧中填充部分数据怎么办?

So while html responses do make sense in some cases, AJAX is lot more efficient if only data in form of JSON is transfered.因此,虽然 html 响应在某些情况下确实有意义,但如果仅传输 JSON 形式的数据,则 AJAX 的效率要高得多。

Mainly it's brevity;主要是简洁; sending only the raw data rather than HTML should result in a significantly reduced data payload.仅发送原始数据而不是 HTML 应该会显着减少数据负载。

Different browsers process JSON with varying speeds depending on whether they have an inbuilt JSON parser.不同的浏览器以不同的速度处理 JSON,具体取决于它们是否具有内置的 JSON 解析器。 Essentially all modern browsers have this - the notable exception (as always!) is IE6 for which a JSON parser shim can be used (see Douglas Crockfords json2.js )基本上所有现代浏览器都有这个 - 值得注意的例外(一如既往!)是 IE6,可以使用 JSON 解析器填充程序(参见 Douglas Crockfords json2.js

It's always possible that you might want to perform some special processing on the client and if you have the data then you have more power to be flexible.您总是可能希望在客户端上执行一些特殊处理,并且如果您有数据,那么您就有更多的能力变得灵活。 Unless there are exceptional reasons why not I would generally make use of this approach...除非有特殊原因,否则我通常会使用这种方法......

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

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