XMLHttpRequest 还是 $.ajax?

发布于 2024-11-18 05:36:48 字数 94 浏览 0 评论 0原文

根据您的经验,您是否认为使用 DOM 对象 XHR 或 ajax 方法与 jquery 更好?

或者也许这取决于性能、可靠性或可用性等方面? 我想听听一些看法

Thanks to your experience, could you say it's better to use the DOM object XHR or ajax method with jquery ?

Or maybe it depends on points like performance, reliability, or usability ?
I'd like to have some views

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

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

发布评论

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

评论(4

梦里南柯 2024-11-25 05:36:48

我总是建议使用 JQuery 等库,而不是使用 httpxmlrequest 编写自己的 ajax 系统。

首先,JQuery的ajax代码已经存在,并且已经过彻底的测试。众所周知,它运行良好,可在所有浏览器中运行,并且安全。

如果您编写自己的 ajax 代码,您将永远无法确定这两点。您永远无法像 JQuery 那样进行如此多的测试。

JQuery 的ajax 代码也非常易于使用。网络上到处都有优秀的示例和教程。如果您自己编写,则需要考虑为其编写文档(考虑几年后必须接管代码维护的人!;-))

另外,如果在 JQuery 中发现任何安全错误,你可以肯定很快就会发布一个补丁。如果您的代码存在安全错误,您可能不会知道它,直到您意识到您的网站遭到黑客攻击。

唯一应该考虑不使用库来完成此类事情的情况是,如果您处于资源极其紧张的环境中。例如,您知道您的站点需要与内存极低或带宽极低的计算机配合使用,这将使下载 JQuery 等库的开销过高。但在这种情况下,您无论如何都不太可能编写支持 Ajax 的代码。

我应该指出,JQuery 并不是唯一可以做这种事情的库。 JQuery 很棒,但如果您想尝试其他东西,还有许多其他具有类似功能的工具。如果您不需要 JQuery 等成熟框架的开销,还有许多较小的特定于 Ajax 的库。

I would always recommend using a library such as JQuery rather than writing your own ajax system using httpxmlrequest.

Firstly, JQuery's ajax code already exists, and has been thoroughly tested. It is known to work well, work in all browsers, and to be secure.

If you writing your own ajax code, you'd never be quite sure on either of those points. You'd never be able to do as much testing as JQuery.

JQuery's ajax code is also extremely easy to use. And there are excellent examples and tutorials all over the web. If you write your own, you'll need to consider writing documentation for it (consider the guy who has to take over maintenance of your code in a few years! ;-))

Plus, if there are any security bugs found in JQuery, you can be sure that there will be a patch released for it pretty quickly. If your code has a security bug, you probably wouldn't know about it until after you realised your site was being hacked.

The only time you should consider not using a library for this sort of thing is if you're in an environment where resources are extremely tight. For example, you know your site will need to work with extremely low-memory computers, or those with very low bandwidth, which would make the overhead of downloading a library like JQuery prohibitive. But in that case, you're unlikely to be writing Ajax-capable code anyway.

I should point out that JQuery isn't the only library out there which can do this sort of thing. JQuery is great, but if you want to try something else, there are a number of others with similar capabilities. There are also a number of smaller Ajax-specific libraries, if you don't need the overhead of a full blown framework like JQuery.

寒冷纷飞旳雪 2024-11-25 05:36:48

最好使用 jquery ajax 方法,因为它有以下优点:

  • 它是跨浏览器的
  • 你不需要关心内部细节(这也因浏览器而异)
  • 如果有错误(很少) )有一个很大的社区可以快速修复它
  • 非常容易理解的代码(您和任何人都可以毫无问题地维护它)

只有当您无法使用jquery ajax执行特定操作时才应该使用普通xhr,例如长轮询(非常罕见的情况)

那是我的 2 美分。
希望这有帮助。干杯

It's better to use jquery ajax method, because of the following advantages:

  • It's cross browser
  • You don't need to care about internal details, (that also vary from browser to browser)
  • If there's a bug (rarely) there's a big community that fixes it quickly
  • Very understandable code (you and anyone can mantain it without problems)

You should use plain xhr only when you can't do something specific with jquery ajax, for example long polling (very rare cases)

Those were my 2 cents.
Hope this helps. Cheers

时光清浅 2024-11-25 05:36:48

就可用性而言,使用 $.ajax 是无与伦比的。很多人都对 jQuery 感到满意,因此您和您的同事将更加准备好处理修改和更新代码。

In terms of usability, using $.ajax can't be beat. So many people are comfortable with jQuery that you and your colleagues will be much more prepared to handle modifying and updating code.

浅忆 2024-11-25 05:36:48

使用库可以简化代码,减少出错的机会,并消除浏览器之间的差异。

我不会特别推荐 jQuery,但我会推荐使用库。

Using a library simplifies your code, reducing the chances of making a mistake, and irons out differences between browsers.

I wouldn't recommend jQuery in particular, but I would recommend using a library.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文