View Full Version : Javascript usage of DOM with IE

10-09-2006, 11:32 PM
I am trying to use the ajaxroutine.js routines. If I use FireFox, everything works great. If I use IE, the responseXML never shows up. The responseText is ok on either browser. I am using IE 7.0 and have tried earlier versions as well. My code is the following:

function processUpdateMenu(){
var myajax=ajaxpack.ajaxobj;
var myfiletype=ajaxpack.filetype;
if (myajax.readyState == 4){ //if request of file completed
if (myajax.status==200 || window.location.href.indexOf("http")==-1){ //if request was successful
if (myfiletype=="txt") {
alert(myajax.responseText + "--text");
// No matter what I put in as the tag name, I always get nothing on IE
var x = myajax.responseXML.getElementsByTagName("xmlresponse");
// the next line returns nothing on IE, an object on FireFox
alert("xml--" + myajax.responseXML);
alert("length: " + x.length ); --- this is always 0 on IE

Any ideas would be appreciated


10-09-2006, 11:56 PM
The website is www.wineryearth.com. If you use FireFox, the menus all work, if IE, they don't.

10-10-2006, 12:58 AM
Your server-side code is returning data marked as HTML (text/html), not XML (application/xml or text/xml - preferably the former). Edit your PHP code to include a header function call that sets the Content-Type HTTP header.

In case you were wondering: Firefox shows no apparent problems because you were overriding the MIME type with the overrideMimeType method. You should never need to call that: if the wrong MIME type is sent, the problem should be solved at the server.

Hope that helps,

10-10-2006, 03:54 PM
I tried adding the header, but it didn't help. I added this to the php:

<meta http-equiv="Content-Type" content="application/xml; charset=ISO-8859-1" />

Is that the right thing to add. If you go to www.wineryearth.com and click on the continent menu you will see the xml that is being returned as text, and then the fact that xml is empty

10-10-2006, 05:04 PM
Is that the right thing to add.No. Change the Content-Type header, don't use a <meta> tag, which is overridden by the header (and has no effect in XML anyway).

10-10-2006, 07:27 PM
<meta http-equiv="Content-Type" content="application/xml; charset=ISO-8859-1" />

Is that the right thing to add.

What Twey said: no. As I wrote in my post, you need to use the PHP header function. Headers are part of the HTTP protocol, sent before the content generated by your code[1]. You need to include:

header('Content-Type: application/xml');

You can add a character encoding (charset) parameter, though you might want to consider UTF-8 (the default for both XML and XMLHttpRequest) over ISO-8859-1. Either way, make sure that the parameter is accurate.

As Twey also mentioned, meta elements containing content type hints are ignored in XML. Indeed, the only things that will care about these even in HTML are browsers, yet they are not the only interested parties. The Web is built on top of the Internet, and you, the developer, have no idea what lies between your server and your visitors. Caching servers and gateways may be very interested in this sort of metadata, which is why it should be the server that sets it.

If you aren't familiar with HTTP, and you intend to work with server-side languages, then, in my opinion, you really should become aquainted the protocol. The specification (RFC 2616) is quite readable, and you can probably find other primers on the Web to help you along (though 2616 really is mandatory reading, in the end).


[1] Hence the header function needs to be called before your code starts writing data to the output stream (unless output buffering is used). If the header function is called later, a warning will be generated.

10-11-2006, 03:34 PM
I changed my php to look like the following: (with some stuff removed)
So now the header is the first thing. Works fine in FireFox, but still not in IE.
I still don't get any xml back to IE.
Any other ideas?

header('Content-Type: application/xml');

function getData($data) {
$data = "<hello />\n";

return $data;
<search><? echo $appelation ?></search>
<data><? echo getData($data) ?></data>

10-11-2006, 04:01 PM
And I do appreciate all the help

10-11-2006, 04:13 PM
Not offhand, no. I can't reproduce the behaviour. If I have PHP return an XML document without setting the Content-Type header, MSIE exposes an empty XML document. If I add the header function call, the document contains all of the elements it should.

You'll need to post the source code for a testable demonstration, though you should also check your server logs (if you haven't already) and double check the response headers to ensure that the Content-Type header has in fact been set properly, first.