Is there any real advantage to using the jQuery AJAX function over the type of AJAX function shown below? Obviously, the syntax is a little cleaner / shorter – but is there any noticiable difference that would justify re-writing my existing ajax code?
var ajax = getXmlObject();
var url= '/addPartToCart.php?m=' + encodeURIComponent(m) +
'&q=' + qty +
'&refresh=' + randomString();
if (ajax.readyState == 4 || ajax.readyState == 0) {
ajax.open("POST", url, true);
ajax.onreadystatechange = function (){
if (ajax.readyState == 4) {
}
};
ajax.send(null);
}
function getXmlObject() {
if (window.XMLHttpRequest) {
return new XMLHttpRequest();
} else if(window.ActiveXObject) {
return new ActiveXObject("Microsoft.XMLHTTP");
} else {
//error
}
}
UPDATE
I’ve reprogrammed all of my AJAX functions to use jQuery’s .ajax(). I must say I’ve seen a notable improvement in speed and reliability.
You may want to give a look to jQuery’s src/ajax.js source code. One reason it’s 720 sloc, and not just 10 is mainly to:
Fix and wrap known inconsistencies between the different browsers. The comments will reveal many of these issues.
Provide extra features like handling JSONP through the same interface, event hooks, etc. And other syntactic sugar.