The XmlHTTPRequest function was designed so that the client can get small pieces of information from the server without having to do a page refresh. Obviously, the developers thought that XML will be the format of choice for this data exchange.
Thank God they didn’t make XML validation necessary!
I say this because XML will not be the format of choice for this data exchange. Let’s see why.
Firstly, let’s see how the data flow will be in the case of XML over XmlHTTPRequest. A typical scenario is where the data lies in a database on the server, and the data is requested by the client. The steps in the process will be:
- Server reads the request, extracts the data from the database.
- Server prepares an XML document using this data.
- This XML is sent to the client.
- Client validates the XML for well-formed-ness.
- Client extracts data from the XML markup.
- Client calls functions/triggers events which use this data as parameters in some way or other.
- Is it necessary to prepare an XML document (step 2 above)? Isn’t that an extra step that is probably not necessary?
- Client validation is necessary (step 4 above) to ensure that the XML parser can parse the XML. This puts heavy processing requirements on the client. Is this really necessary?
- The data from the XML will then have to be extracted (step 5 above). This isn’t really a necessary step either if the data is not marked up as XML. So, then, is it really necessary to use XML?
So, in what format will data be transferred over XmlHTTPRequest? The simplest way is as a plain old string of data. This will be the most efficient as far as download speeds are concerned. You will download only 11 bytes where 11 bytes of download is required, not more. Besides, there won’t be any XML parsing or validation in the picture, making the application that much faster.
Let me use an example from the guy who deconstructed Google Suggest, and the way XmlHTTPRequest works there. According to him, the code returned by Google when you hit a keystroke looks like this.
sendRPCDone(frameElement, "fast bug", new Array("fast bug track", "fast bugs", "fast bug", "fast bugtrack"), new Array("793,000 results", "2,040,000 results", "6,000,000 results", "7,910 results"), new Array(""));
Basically, what this code does is call a function, with a bunch of parameters. The function called,
evalfunction to execute the function on the client side (