What the File System Doesn’t Tell You
In my previous post, I described the practice of mocking the entire server-side tier of an Ajax application with a static XML file. This is just a practical note to point out a couple of gotchas that can arise when doing things this way. The seasoned pros out there will know this anyway,but it can trip up the unwary.
responseXML and mime types
The XmlHTTPRequest will offer up the response in two formats. responseText gives you the data as plain text, and responseXML as a DOM-style Document object that you can query using all the usual methods and properties such as childNodes(), firstChild, getElementById() and so on.
responseXML will only be set if you provide a mime type of ‘text/xml’ (or ‘application/xml’ or whatever) that clearly indicates that the response is an XML document. Even if it is well-formed XML with a <?xml?> header, responseXML won’t look at it unless the mime type is set.
So, serve your mock from a web server,
The same issue applies to XSLT transformations by the way. Both the XML data feed and the stylesheet need to be returned as an XML mime type.
HTTP Status of Zero
In a lot of XMLHttpRequest wrapper libraries, the onReadyState event handler for an XmlHttpRequest object will often check the httpStatus of the response, and forward on to another handler if the status is 200. When seved off the file system, the statis typically evaluates to zero, which can be another reason for using a simple web server like Apache.
So, to sum these two points up, don’t use the file system for your mocks. If you’re running Linux, youll already have Apache installed, or else it’ll be a simple apt-get or rpm away. Windows users looking for a simple apache setup can either download the server installer from Apache themselves, or get the full LAMP stack from XAMPP. The setup for both of these is very easy, and once they’re running, they take very little looking after (integration as a windows service is practically automatic), and just drop your test code onto the /htdocs folder to test it out.
[…] r you, but XHR gives you much more flexibility. I’ve blogged on some aspects of this before before. Here are a few more quick tips: HTTP d […]
Pingback by classical geek » Blog Archive » What you need to grok to grok Ajax — February 10, 2006 @ 8:48 pm