简体   繁体   中英

XMLHttpRequest or $.ajax?

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

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.

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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