简体   繁体   中英

Traditional Ajax or Jquery Ajax, which one is better?

This is just a general question. Which is better, traditional ajax or the jquery ajax? Because when I use the jquery ajax, it seems to do funky weird stuffs in IE. And it supposed to be a cross-browser library... So I'm a bit confused as to which one to use. Again I'm asking this because of the serial-killer IE...

The funky weird stuff, ie sometimes it stops sending information to the server after just a few requests, even when I put 'cache: false'.

There's not a meaningful difference. jQuery's ajax() is naturally built on XMLHttpRequest and they should behave the same. I think some other scripting error is causing the “funky weird stuffs”.

Note if you want to do cross-browser XMLHttpRequest without a large JS library, it's actually pretty simple these days. All you need is the fixup for IE6:

if (!window.XMLHttpRequest && 'ActiveXObject' in window) {
    window.XMLHttpRequest= function() {
        return new ActiveXObject('MSXML2.XMLHttp');
    }
}

and then you can use the standard new XMLHttpRequest interface anywhere. There is no longer any need to deploy the overrideMimeType hack (which was for ancient Mozilla milestone builds), and there never was any need to try multiple ActiveXObject progids.

Easy. JQuery.

Downside:

  1. Extra script load

Upsides:

  1. Standardizes the API across all major browsers
  2. Consistent < 2 min wait for an answer here on SO for any question you have :)
  3. Much enhanced functionality

jQuery $.ajax() should work fine in IE. It uses XMLHttpRequest just like what you'd use if you were to do it yourself. But jQuery's method has grown little hairs and stuff on it to make it work consistently between different browsers, as much as possible.

Give a look to jQuery's ajax source code . One reason it's 593 sloc , and not just 4, is to fix and wrap all the inconsistencies between the different browsers. The comments will reveal many of these issues.

You'll obviously have to spend time to learn the jQuery framework but the upshot is that jQuery is one of the most used JS frameworks. So your investment in time will pay off as you'll find jQuery can do other things than just Ajax communication.

Also the way that jQuery allows traversing the DOM tree is supreme which makes developing things in jQuery very simple.

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