views:

101

answers:

4

Hi All

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, i.e. sometimes it stops sending information to the server after just a few requests, even when I put 'cache: false'.

+2  A: 

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
sje397
+2  A: 

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.

Daniel Vassallo
I'd say most of the lines in jQuery is for producing syntactic sugar, though (while of course quite a few loc have to do with browser quirks..)
npup
Actually there is very little browser-workaround code in `ajax.js`. Most of the work there is for the extra features, like handling JSONP through the same interface, event hooks, or using `load()` to inject content into the DOM (via a horrendous and ill-advised regex hack).
bobince
@bobince, @npup: You're right. Rephrased my exaggerated statement :) ... However, there are quite a few interesting comments of the form "This is because Opera doesn't do this...", "IE won't fire here", "Firefox 1.5 there", etc...
Daniel Vassallo
+2  A: 

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.

Am
+5  A: 

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.

bobince
@bobince: The thing is when I check the error console in firebug, it doesn't give any sign of error
Shaoz
Put some `debugger` commands in to see what code's being executed? If `ajax()` is actually getting called it should definitely result in a request.
bobince
You can also use something like Charles Proxy or Wireshark (I prefer the former) to watch HTTP traffic. This will tell you if requests are being made or not, and what exactly the requests and responses look like
Justin Johnson
Shaoz
IE8 has a fine debugger. (Older versions have to use separate debuggers like [MSD](http://en.wikipedia.org/wiki/Microsoft_Script_Debugger)).
bobince