admin管理员组

文章数量:1401247

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

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

Share Improve this question asked Jul 2, 2011 at 20:07 vdegennevdegenne 13.4k17 gold badges85 silver badges116 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 7

I would always remend 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 puters, 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 munity 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 fortable 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 remend jQuery in particular, but I would remend using a library.

本文标签: javascriptXMLHttpRequest or ajaxStack Overflow