As I've become more proficient with intranet application development, I've come to love certain aspects of intranet development over Internet development. In particular, I love that I have full control over the users' experience. I work for a small company (roughly 60 people), and only about 15 of those people use the intranet apps I develop. We're all based in the same building, so it's been very easy for me to ensure that all users are using the latest version of Firefox with javascript enabled.
Anyway, I've recently begun revisiting some of my earlier applications in an effort to improve performance. One way I'm doing this, at times, is by incorporating some AJAX (please forgive the buzzword usage). When I first tried AJAX a year or so ago, I was pretty confused, and couldn't really find many tutorials that were simple. As I realized more how I could benefit from proficiency in this area, I finally bought a book: Pragmatic Ajax. The book simplified things further than any other I looked at, and it actually embarasses me to say that I had trouble understanding at one point.
Now that I've experimented with various ways of processing AJAX requests, I'm torn as to which method I prefer. Here are three I've tried:
Returning plain text, and using javascript to create XHTML.
This is by far the simplest method, and the first I tried. Basically, I would make a request to a PHP page that simply printed some plain text on the page, and then I would use javascript to format that text as I needed. Even if I was retruning a recordset from a database, I would use PHP to print the records in some character-delimited format, and parse the records with javascript.
Returning well-formed XML, and using javascript to create XHTML.
This method takes a little more work on the PHP side, and puts a bit more strain on the server, but theoretically may simplify the javascript a bit. In my experience, this method works well, but just isn't as easy for me. Maybe I just need to learn more about parsing well-formed XML with javascript, but this is my least favorite method.
Returning XHTML and inserting it directly into the DOM.
Recently, I've taken to using PHP to create a block of XHTML markup, and then simply used javascript to reference a DOM object to contain the XHTML. This is the heaviest load on the server, and may not be practical for large-scale websites, but it works really well for me, and simplifies the javascript a ton.
I probably prefer generating XHTML through PHP because I'm more comfortable developing in PHP than javascript, but I want to try to avoid doing what's comfortable and start paying more attention to best-practices.
How do you all perfer making and handling AJAX requests? What do you think of my notes and thoughts? All suggestions are much appreciated.
The Ajax model is only scary until you dive into it. It's like an ice cold pool. You know you want to jump in, but you're worried about the initial slowing of your internal organs. But once you jump in and find your blood flowing again, you start to enjoy the experience.
My favorite method is to return well-formed XML, crawl through it with some DOM functions alongside some custom ones, and return the visual representations back to the user. I've heard of PHP libraries built specifically for Ajax, for which I just don't see any need. All you really need to do is create XML templates alongside your XHTML templates, and you can often use the same PHP models for retrieving data and such.