How do you do basic ajax?

This is a post for all web developers who’re on my friends list or passing by my journal.

So I’ve been playing with Ajax related technologies for a little over a year now and there seems to be so many ways of doing things that it’s become this mish mash of things good and crap. The way I like to use Ajax is making use of XmlHttpRequest or the ajax framework of choice to make the calls to the server end, get the appropriate data (or process it if need be on the server end) and then doing some UI rendering on the client end after that. Now, the natural way to do this of course is in a javascript callback function where you’d start modifying elements in your page. Question is, how do you do that? I’ve come across three (well, I had one sort of literally shoved down my throat that left a bad after taste) and I’ll write them out.

1. Get back the response object, and then insert the data appropriately by making the xml dom object methods.

2. Get back the response object, and then create a whole bunch of html in a string and pass that into the innerHTML of some div element. This is still acceptable for me where I might have to consider the implications of browser-specific issues with dom method usage. The classic one I’ve found is when I have to write setAttribute(“class”, something) AND setAttribute(“className”, something) for it to work on IE and Firefox. If I just wrote a string and passed it to innerHTML, I can just write the html in it that would be more browser friendly than the dom methods.

3. Get back the response object which itself has presentation logic that the client just pass to innerHTML of a UI element. Am I being snobbish or elitist to think that this is stupidness? Or maybe it’s the object oriented programmer in me squirming to still maintain elements of object oriented programming in a programming domain where you have to use a whole bunch of hacks to do anything that resembles good coding practices (which then nullifies the effort to bring about some order to the way we integrate html with back end systems). It’s not that I’m just being a purist in not wanting to receive presentation content directly from a server call that was meant to be a search or an action. It’s just that this approach seems to make programming more like taking a whole bunch of darts and flinging them at a dartboard and hoping that somehow they all stick in the right order in the right place. Debugging any problems becomes a nightmare. Not only that, whoever’s going to come maintain this code after you leave is going to curse you for being an insensitive git.

So here’s my question to all you web developers out there. What’s your take on all of this? I think web developments is one arena where programmers with various different backgrounds mingle and can share ideas. I’d like to hear your arguments to sway me to the third option above. (or hear validating arguments to let me know that I’m not insane).

How do you do basic ajax?

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s