Ajax HTTP Gotcha
While testing out some code for the book a few days ago, I came across an interesting problem. I’ll share it hrere, in the hope of sparing a few other Ajax developers from the confusion that I was experiencing.
The code that I was testing performed a client-side XSLT transformation, using XmlHTTPRequest to grab the XML data (which was generated dynamically from a PHP script, if memory serves me right), and the XSL stylesheet, which was a static resource. The request stuff was abstracted out using the prototype.js Ajax.Request object.
new Ajax.Request( this.xslURL,
{
onComplete: this.setXSLDocument.bind(this)
}
);
The code worked fine on my test server (Mandrake Linux’ modified version of Apache 2.0.something), so long as the stylesheet had a fileextension of .xsl, identifying it with the correct mime type in the relevant config file. However, my proofreader colleague couldn’t get it to work on his test server, which was Apache 1.3.27, running the same version of PHP as I was. The request for the XSL stylesheet was greeted with the following:
HTTP Error 405
405 Method Not Allowed
Not something that I’d come across before. It’s a very helpful error response, which sends a HTTP header back listing which methods are allowed, in this case GET, PUT, DELETE and HEAD, but it was getting in my way. Eventually, after much googling, we located some references to a known issue with Apache mod_cgi on the 1.3 branch, whereby it would sometimes make curious decisions about which method to allow. Floating up in the cosy scripting language layer, I sometimes forget that the millions of lines of infrastructure code that I rest upon are written by people too.
Anyway, we decided that the code worked, and put my colleagues problems down to configuration issues. Then I realised how trivial it would be to fix the symptom, even though I couldn’t fix the Apache bug. Changing my code to this:
new Ajax.Request( this.xslURL,
{
method:"GET",
onComplete: this.setXSLDocument.bind(this)
}
);
provides the ideal workaround, and happens to be more correct anyway. The stati resource should be fetched using GET, as any RESTful afficionado will be quick to point out. Prototype (and Rico, and Sarissa, and the helpers we develop in the book) correctly default to POST, because the majority of Ajax use-cases will involve the server being hit for dynamically generated data. And correctly, they provide simple configuration options for overriding the default.
My bad for not thinking to use them in the first place, but at least I know what a 405 HTTP status code means.
Thank you so much for posting this issue…it certainly helped me out. Big time.
Comment by Cody Lindley — December 1, 2005 @ 3:40 pm
I just stumbled upon this issue and your post helped me out.
Thanks.
Comment by Jacek Becela — March 9, 2006 @ 2:45 pm
Thanks so much for this post. I had the same problem with IIS, i works now
Comment by Jean-Baptiste Fradin — January 25, 2007 @ 5:28 pm