Avoid JQuery.post() or JQuery.get() If Possible. Use JQuery.ajax() instead

Over the past few several months, work has been crazy developing a JS intensive application for one of our clients. It been going great and I’ve finally found a need to use Node.js and Websockets after reading so many good things about them.

During development as Team Lead on the project, I’ve taken the opportunity from time to time to review the code being written for various parts of the application, especially when things break. I noticed that most often when some feature broke, it was usually because of some AJAX call that failed to load something. Upon inspection, I would find that $.post() or $.get() is being used to make the AJAX calls. I would usually fix the code by using $.ajax() instead, and the “error” callback to determine what is failing in the response from the server.

I noticed most often that $.post() or $.get() was used because it was more convenient to code, however, I believe its usage is too trusting of the response of the server, which for many reasons could fail to return the right response. $.ajax() may be more verbose to type, but I feel is a better option because you can explicitly handle the error condition if the response should not be as expected.

If you would want to use $.post() and $.get() for syntactical brevity, then I would recommend you setup your global JQuery AJAX options before hand, to handle the error conditions, but I believe that is not the best option in all cases, because sometimes you need to have fine grained control of error handling.

So to conclude, I recommend, staying away from $.post() or $.get() when doing heavy duty AJAX communication in which error handling should be priority, and using $.ajax() instead.

I would be glad to hear other experiences and whether or not this view is supported by the larger web application development community.


While you here, take a break from your hard coding life to enjoy some Twi Pop music.