AJAX vs AHAH 有性能优势吗?
我关心的是性能,是否有理由发送客户端 XML 而不是有效的 HTML?像大多数事情一样,我确信它取决于应用程序。我的具体情况是,从数据库中提取的大量内容被插入到网页中。
这两种方法有什么优点?内容的大小是否值得关注?或者,在使用 XML 的情况下,JavaScript 将 XML 处理为 HTML 的时间是否会抵消发送 HTML 所需的额外时间?
谢谢,杰夫
My concern is performance, is there a reason to to send the client XML instead of valid HTML? Like most things, I am sure it is application dependent. My specific situation is where there is substantial content being inserted into the web page that has been pulled from a database.
What are the advantages of either approach? Is the size of the content even a concern? Or, in the case of using XML, will the time for the JavaScript to process the XML into HTML counterbalance the extra time that would have been required to send HTML to start with?
Thanks, Jeff
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
多年来,AJAX 一直没有被严格遵循其缩写。现在它只是“异步加载内容”的一个绰号。
现在大多数 AJAX 都是使用 JSON 完成的。
无论您是否使用 HTML 作为数据而不是 JSON 或其他任何内容(甚至 XML),实际上取决于您的应用程序的特定需求。从这方面来说,AHAH 实际上只是 AJAX 的一个子集。
如果客户端解析/渲染数据没有任何好处,那么只需让服务器端执行此操作并返回 HTML。
AJAX hasn't been strictly followed per its acronym for years. It's just a moniker now for "asynchronously loaded content".
Most AJAX these days is done with JSON.
Whether or not you go with HTML as your data over JSON or anything else (even XML) is really up to the specific needs of your application. In that respect, AHAH is really just a subset of AJAX.
If there is no benefit to be gained from having the client-side parse/render the data, then just have the server-side do it and return HTML.
重要的是要认识到,即使在获取 HTML 时,也不应该直接将其粘贴到 DOM 中而不对其进行处理。如果不通过安全协议传输,则可能会受到 MITM 攻击。解析 XML 所节省的时间都花在清理 HTML 以防止脚本注入上。
XML 也更加可移植。 XML 中提供的数据可以在任何页面的任何位置使用。 HTML 在这方面有所不同(尽管它可以使用 CSS 进行样式化),并非所有浏览器都有现成的解析器,无需将内容插入 DOM。有些可能有
DOMDocument()
,但您不能依赖它来实现跨浏览器目的。通过 XML,您还可以使用 XPath 轻松定位和提取所需的数据。对于 HTML(x 浏览器),您需要首先插入到 DOM 中,或者使用非常不可靠的 regexp 方法,这种方法实际上不应该被使用。JSON 更加轻量级,它消除了 XML 和 HTML 带来的大量内容。作为 JavaScript 对象的本机标记,它也很容易解析。与 XML 和 HTML 不同,您还可以使用 JSON with Padding (JSONP) 跨域访问 JSON 数据。
总之,您需要选择最适合您需求的方法。如今,大多数人倾向于使用 JSON,因为它非常轻量,可以跨域访问,并且只需很少的精力即可用 JavaScript 进行解析。
It's important to recognize that even when fetching HTML you shouldn't just stick it straight into the DOM without processing it. If it's not transmitted through a secure protocol it could be subject to MITM attacks. Any time saved parsing XML is spent on sanitizing the HTML to prevent script injections.
XML is more portable too. The data served in XML can be used anywhere on any page. HTML is different in this aspect (although it can be styled with CSS), not all browsers have a readily available parser without inserting the content into the DOM. Some may have
DOMDocument()
, but you can't rely on it for cross browser purposes. With XML, you can also target and extract the data you need effortlessly using XPath. With HTML (x-browser), you'd need to insert into the DOM first or use the ever-so-unreliable regexp method that really shouldn't ever be used.JSON is more lightweight, it does away with a lot of the bulk that comes with XML and HTML. Being native markup for JavaScript objects, it's also very easy to parse. Unlike XML and HTML, you can also access JSON data cross domain using JSON with Padding (JSONP).
In summary, you need to choose the method that best suits your needs. Most people tend to go with JSON these days because it's very light weight, can be accessed cross domain and requires very little effort to parse with JavaScript.
JSON(Javascript 对象表示法)比 XML 更常用,因为它是轻量级的,并且是原生 Javascript。
话虽这么说,如果您需要的是 XML,那么您将撤回 XML。
这是一个关于何时使用 HTML、XML 或 JSON
这是一个 好页面关于两者之间的差异。
使用 JSON 主要是出于性能原因。要使用该页面中的示例:
XML:
JSON:
使用 JSON,冗余要少得多。
OTOH,发送纯 HTML 有时也非常有效。您必须考虑您的数据。如果您只是更新一段文本,只需发送 html 即可。如果您正在处理要在 Javascript 中以某种方式操作或使用的项目或项目集合,那么您需要 JSON。如果您想异步更新 RSS Feed 或其他一些 XML,则需要 XML。
请记住,HTML 只是 XML 的一个子集。 xHTML 遵循所有 xml 规则。支持 javascript 的浏览器(所有浏览器)都可以理解 JSON (Javascript) 和 HTML (XML)。根据您将如何使用数据选择适合您的项目的内容。
JSON ( Javascript Object Notation ) is more often used than XML because it is lightweight, and native Javascript.
That being said, if what you need is XML, then you would pull back XML.
Here's a good page on when to use HTML, XML or JSON
Here's a good page on the differences between the two.
JSON is used for performance reasons, mostly. To use the example from that page:
XML:
JSON:
With JSON, there is far less redundancy.
OTOH, sending plain ol' HTML is very effective at times as well. You have to think about your data. If you're just updating a paragraph of text, just send html through. IF you're dealing with items, or a collection of items that you're going to manipulate or use somehow in Javascript, you want JSON. If you want to ASyncronously update your RSS Feed or some other XML, you ask for XML.
Remember, HTML is just a subset of XML. and xHTML follows all the xml rules. Browsers that are javascript aware ( all of them ) can understand JSON ( Javascript ) and HTML (XML). Choose what fits your project based on how you will use the data.