Internet Explorer AJAX Gotcha: “Object Required” Error
by Alistair Miles
I discovered (eventually) that, if the response to the XML HTTP request does not have Content-Type “text/xml”, then MSXML does not instantiate the responseXML member of the XMLHTTPRequest object, although it will instantiate the responseText member – see the MSXML2 API spec. Once I had fixed the server-side code to return the correct Content-Type, it worked. As an item of good-practice, you should probably include an “Accept: text/xml” header in the request, to be on the safe side.
However, the story doesn’t quite end there. I also ran into some subtle HTTP proxy and caching issues that I don’t fully understand. If you try the above fix, and change your server-side code to return “Content-Type: text/xml” in the response headers, but are still having problems, you should run a test script to check the response headers that your AJAX application is actually receiving. You can get a dump of all the response headers by called the getAllResponseHeaders() method of the XMLHTTPRequest object.
I found that, even after I had fixed the server-side code, my AJAX application was still receiving a mysterious “text/html” response on some computers, although this problem disappeared when I fiddled with the HTTP proxy settings in IE. The problem also disappeared when I tunnelled the request through Nettool. As I said, I don’t fully understand what’s going on, but you might want to clear all temporary internet files in IE before retrying your app, and you might want to include suitable HTTP headers in your XMLHTTPRequest to avoid receiving cached representations. You might also want to include suitable HTTP headers in the server response to prevent caching.
As an aside, I initially thought that the problem was something to do with the fact that my server-side component is running in Tomcat, which is acting as a worker for Apache 2. However, I ruled that out when I found the problem disappeared when I fiddled with the proxy settings in IE. I did discover, however, that you can get Apache to modify the HTTP response headers after a request has been handled by a worker. Below is an extract from my Apache config file that I used to experiment with modification of headers.
Header set Cache-Control "max-age=0, no-store" Header set Foo "bar" Header set Content-Type "foo/bar" ForceType "foo/bar" JkMount /oai* tomcat55
I discovered that you can use the “Header” directive to set almost any of the response headers, but not the “Content-Type” header if it has already been set. The “ForceType” directive also has no effect … I.e. the last two lines inside the “Location” section have no effect. The Apache 2 docs mention “header filters” but I couldn’t find any more info about this.