2

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.

2
  • Much more parameterizable (not a word but you get the meaning) Commented Nov 24, 2010 at 21:00
  • I see your point. Often times I just serialize a form, and stick onto the url url += '&formJSON='+ encodeURIComponent(serializedFormString); Commented Nov 24, 2010 at 21:02

5 Answers 5

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

I'm not sure that it would be worth re-writing unless you are already using jQuery for other parts of your website. The primary advantage is that the code is a lot easier to read with jQuery, and it takes care of cross-browser compatibility issues for you.

Using jQuery solely for AJAX may not be the best idea though.

Comments

0

Being a newbie to JS and Jquery I can say that jQuery.Post(url); is a million times easier to read than above...:-)

1 Comment

of course, but there's more to it than your code, if I need to act on on the response. I could eliminate half of my code if I just needed to send the message.
0

It really depends on if you need the additional features that jquery ajax provides. I tend to send data as object literals instead of string concats. Also one feature I use very often is the ability to evaluate script tags within the response html. jQuery ajax handles these things very nicely.

If you are happy with the code the way it is then stick with it. I think you would find many places (besides ajax) to implement jQuery if you included it. :)

Comments

-2

yes, and goto's w/ if statements can do everything a while loop, for loop, list comprehension, or an array map/filter/reduce can do.

1 Comment

downvote me if you want but its the same thing. expressiveness/brevity/clarity with jQuery's ajax or particularly $.load is a hell of a lot better than writing out the entire ajax pattern seen in the question just like an array map is better than using lots of ifs and jumps and indexing

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.