Ajax:定义与实现? (XML vs JSON vs 其他)

发布于 2024-07-13 14:36:38 字数 259 浏览 9 评论 0 原文

AJAX实际上意味着异步Javascript和XML。 这个术语的产生是因为(据我所知)第一批启动此过程的人通过 XML 将数据从服务器传输到客户端。 最近(自从我实际开始使用它以来),JSON 已经出现并且似乎是 XML 的真正替代品。 从我的(可能是微薄的)测试和经验来看,JSON 更小、更容易、更适合传输数据。

所以我的问题是:您使用哪个与服务器/客户端进行通信(可能是为什么)? 或者,您听说过哪些“最佳实践”(以及为什么)?

谢谢大家!

AJAX actually means Asynchronous Javascript And XML. This term was derived because (as I know the story) the first people who started this process transferred data from the server to the client via XML. Lately (well ever since I've started using it actually), JSON has been around and appears to be a real alternative to XML. From my (possibly meager) tests and experience, JSON is smaller, easier, and better for transmitting data.

So my question is this: which do you use to talk to and from the server/client (and possibly why)? Or, what are the "best practices" that you have heard of (and why)?

Thanks all!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

滿滿的愛 2024-07-20 14:36:38

我们通常更喜欢 xml,因为它比 JSON 具有一些优点,例如:

  • Xml 可以被验证,而 json 没有验证器
  • json 中没有命名空间 与
  • json 不同,Xml 是可扩展的
  • Json 通常被认为是不安全的

另一方面 json 本身也有其优点:

  • Json 更简单
  • 它是在客户端更容易处理
  • 当出现问题时调试可以轻而易举

最终这取决于您要开发的内容,对于面向数据的东西我
更喜欢 json。

We generally prefer xml because it has some advantages over JSON like:

  • Xml can be validated while json has no validator
  • No namespaces in json
  • Xml is extensible unlike json
  • Json is generally considered insecure

On the other hand json itself has its advantages:

  • Json is simpler
  • It's easier to process on the client side
  • Debugging can be a breeze when something goes wrong

In the end it depends on what you are trying to develop, for data-oriented stuff I
prefer json.

单身情人 2024-07-20 14:36:38

使用哪一种更好取决于具体的应用/要求。 在浏览器端 JSON 是更好的选择,而在服务器端文件 XML 是更好的选择。 使用正确的工具完成正确的工作。 JSON 更偏向数据,而 XML 更偏向文档。 浏览器 Javascript 代码原生支持 JSON。

有关本文的更多详细信息,请参阅链接,其中对每个示例进行了更好的比较。 http://codebucket.co.in/which-one-is-better-xml -或-json/

Which one is better to use is application/requirement specific. On the browser side JSON is the better choice while on the Server side for files XML is better choice. Use right tool for right job. JSON is more towards data while XML is more towards document. JSON is natively supported by the browsers Javascript code.

For more and detailed info on this post please refer to a link which have more nice comparison of each with examples. http://codebucket.co.in/which-one-is-better-xml-or-json/

櫻之舞 2024-07-20 14:36:38

JSON 与 XML 似乎是一个持续的争论 - 我宁愿选择 JSON,因为它更适合支持混搭和 AJAX 小部件的 Web 服务,因为它本质上是序列化的 Javascript 对象(因此很容易与 Javascript 一起使用)。

您可以在 JSON 或 XML 上找到 JSON 和 XML 优缺点的广泛比较,选择哪种格式?AJAX 响应:XML、 HTML 还是 JSON?

JSON versus XML seems to be an ongoing debate - I'd rather go with JSON since it is a better fit for Web services that power mashups and AJAX widgets because it is essentially serialized Javascript objects (and thus easy to use with Javascript).

You can find some extensive comparison of the advantages and disadvantages of JSON and XML on JSON or XML, Which Format to Choose? and The AJAX response: XML, HTML, or JSON?

终陌 2024-07-20 14:36:38

我更喜欢尽可能使用 JSON:

  1. 它已经作为 Javascript 对象出现,因此您只需调用 eval() 即可获取其所有数据。
  2. JSON 是 Javascript,因此对于已经了解 JS 但不了解 XML 复杂性的人来说,它会更快地理解。
  3. 在浏览器中,您不必经历解析 XML 对象的痛苦。 (您也不必在服务器上构建一个,但无论如何您都必须创建 JSON。)
  4. 这是一种更紧凑的数据传输方法。

I prefer using JSON when possible:

  1. It already comes as a Javascript object, so all you need to do call eval() on it to grab all its data.
  2. JSON is Javascript, so it's faster to grok for people who already know JS but don't have the intricacies of XML down.
  3. In the browser, you don't have to go through the pain of parsing an XML object. (You also don't have to build one on the server, though you do have to create the JSON anyways.)
  4. It's a more compact method of transferring data.
烟若柳尘 2024-07-20 14:36:38

这些天我尽可能倾向于 JSON。 与客户端编码结合起来,它确实感觉更轻,更“自然”。

安全最佳实践:永远不要在不对收到的字符串进行检查的情况下盲目地评估 JSON,因为您可能会执行某人“放置”到 JSON 字符串中的任意代码。

Douglas Crockford 写了一套很好的一套安全原则 使用 JSON 时。

I gravitate towards JSON whereever possible these days. It's certainly feels lighter weight and more "natural" in conjunction with client side coding.

Security best practice: never blindly eval JSON without performing a check on the string received as you could be executing arbitrary code someone has "placed" into the JSON string.

Douglas Crockford has written a good set of security principles when working with JSON.

夜雨飘雪 2024-07-20 14:36:38

当我看到 AJAX 这个词时,我总是微笑。 我笑了,因为早在 XMLHTTP 出现之前我就开始使用相同的技术,更不用说 AJAX 了。

我们在 IE 中的 Intranet 应用程序上执行了与 AJAX 相同的操作,但使用 VBScript 和 Java applet 而不是 XMLHTTP。 我们使用了类似于 JSON 的东西,但是采用 VBScript 语法。

I always smile when I see the term AJAX. I smile because I first started using the same technique long before even XMLHTTP existed, let alone AJAX.

We did the same thing as AJAX on an Intranet application in IE, but using VBScript and a Java applet instead of XMLHTTP. We used something similar to JSON, but in VBScript syntax.

绾颜 2024-07-20 14:36:38

理论上,由于验证等原因,XML 会很快变得好起来。 实际上,您无法在客户端根据合理的 DTD 或模式进行验证(或者在大多数情况下根本无法验证)。 尝试一下你就会看到。

XML 还受到其冗长和客户端消耗的影响。 除非您计划使用 XSLT 或其他东西并直接使用接收到的数据包,否则您必须使用 DOM 来分割数据。

最后,XML 在完成之前无法进行合理的查看(至少在客户端),因此您无法进行部分检查。 但公平地说,JSON 也会面临挑战,具体取决于它的结构方式。

考虑到消费环境(主要是 JavaScript),JSON 具有明显的优势。 在《Ajax:完整参考》(http://ajaxref.com)仅涉及数据类型。 例子证明处理起来并不容易,特别是在鲜为人知的情况下。 如果我必须选择一个 JSON,那就是它,但坦率地说,对于 Ajax 小 HTML 片段的某些用途(因为它是 slap and go)是这样做的方法。

In theory XML would be quick good because of validation and what not. In practice you can't client side validate against a DTD or schema reasonable (or in most cases at all). Try it you'll see.

XML also suffers from its verbosity and consumption client-side. Unless you plan on using XSLT or something and directly consuming the received packet you have to chop the data up with the DOM like made.

Finally XML cannot reasonable looked at until it is complete (client-side at least) so you cannot do partial inspection. Though to be fair JSON will have challenges there as well depending on how it is structured.

Given the environment of consumption (JavaScript mostly) JSON has the obvious leg up. There is a massive discussion of this choice plus other things like YAML, CSV, base64 encoded data, HTML fragments, etc. in Chapter 4 of Ajax: The Complete Reference (http://ajaxref.com) which is just on data types. Examples prove disprove ease of handling particularly in the lesser known cases. If I had to pick one JSON would be it but frankly for some uses of Ajax small HTML fragments (since it is slap and go) is the way to do it.

许仙没带伞 2024-07-20 14:36:38

您还可以生成 html 并将该输出直接使用到您的应用程序中。 XML 非常冗长,而 JSON 出于安全考虑在处理时需要额外注意。

并没有真正的“最佳实践”,就我个人而言,如果我要在 JSON 和 XML 之间进行选择,我会使用 JSON。 尽管如果使用 XML,您可以进行一些有趣的查询。

You can also just generate html and use that output directly into your application. XML is very verbose, where JSON requires extra attention in processing it due to security.

There is not really a 'best practice', Personally if I would choose between JSON and XML I would use JSON. Although you have some interesting query posibilities if you use XML.

ぽ尐不点ル 2024-07-20 14:36:38

我认为关于 JSON 与 XML 的争论表明 JSON 可以成为最佳实践。 我认为客户端-服务器传输速度问题不会很快消失(如果有的话)。 因此,我认为 JSON 将在较小的问题上获胜(我相信 JSON 在可以发送到哪个服务器方面也具有灵活性,但这自然涉及到前面提到的安全问题)

也许我们应该开始称其为“AJAWX”(不带 Xml 的异步 JAvascript)。

I think the debates on JSON versus XML show that JSON can be a best-practice. I don't think that client-server transmission speeds are going to go away as an issue soon, if ever. So I think that JSON will win on the issue of being smaller alone (I believe that JSON also has flexibility as to what server it can sent to but this naturally involves security issues as mentioned)

Perhaps we should start calling it"AJAWX" (Asynchronous JAvascript Without Xml).

伏妖词 2024-07-20 14:36:38

我的选择是 JSON。

因为:

  • 它快得多。 您只需评估响应中的 JSON 代码,而不是解析 XML。
  • 它需要的代码少得多。 eval() 少于 XML 解析器代码。
  • 客户端和服务器之间的流量(传输的数据量)更少,因此处理速度更快,
  • 更具可读性

My choice is JSON.

Because:

  • it is much faster. You need just evaluate JSON code from response instead of parsing XML.
  • it require much less of the code. eval() is less than XML parser code.
  • less traffic (amount of data to transfer) between client and server as result faster handling
  • it more readable
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文