views:

57

answers:

2

Trying to do a basic XML retrieval. The code works as expected in Firefox and Opera, meaning it alerts with the text value of the "title" node from the XML document. But in IE7, I am getting "object required" from this line.

  x=xhttp.responseXML.getElementsByTagName("title")[0].childNodes[0].nodeValue;
  alert(x);

Btw, it was working fine with responseText (and a txt file). But I had to use ActiveX Object to get that working in IE...strange b/c I thought it supported the XMLHttprequest object. Maybe this has something to do with it?

[edit]

ok I replaced the line with this

    x=xhttp.responseXML.childNodes.length;
    alert(x);

in FF there are 2 nodes, in IE there are 0. So obviously it can't read 0 childNodes. Has something to do with whitespace, I guess? What gives?

[update]

It is all related to my ignorance of how to use msxml.DOMDocument and Msxml.XMLHTTP ActiveXObjects. I am learning about these Objects at the following link:

Program with DOM in JScript

and will answer my question in a few days...

A: 

There's no reason why that shouldn't work, for example:

var xml = response.responseXML, 
    node = xml.getElementsByTagName('child')[0];

alert(node.childNodes.length);

Using the following XML

<root>
    <child>
        <foo />
    </child>
</root>

When I run that in IE7, it correctly shows 1. Are you setting the content type of the response properly to text/xml?

Evan Trimboli
+1  A: 

I figured it out. I didn't mention that so far I am developing this out of folders on my hard drive. That was the problem. I would like to quote "Ultimator" whose answer I discovered on webdeveloper.com at this thread: http://www.webdeveloper.com/forum/showthread.php?t=147342

IE7's native XMLHttpRequest is powerless even to a file in the same directory, yet, in the same situation, ActiveX would be able to serve IE7 normally. The reason is because IE7's XMLHttpRequest object is limited to the web. Hence your "access denied in IE7" error since IE7 in your code is being served via XMLHttpRequest rather than ActiveX. Firefox's XMLHttpRequest object, on the other hand, allows both local requests as well as requests on the web.

Actually, I don't even understand why the object required was happening. For a while I switched to using only the msxml.DOMDocument.3.0 Object, and I got that working locally. Then I kept reading about how XMLHttpRequest was supported in IE7, so I went back to working on that object. After reading through tutorials on MSDN, I noticed they suggested to create a "virtual directory" and use the files on

http://localhost.

Then I had the idea, well why can't I just point the Request to somewhere on the internet? And I did: to a sample XML file on w3schools. Guess what? It worked in IE7 and I was able to pull the text I wanted with selectSingleNode. But in Firebug it said, xmlDoc.responseXML is null. Interesting...

So ultimately I decided to activate IIS on my machine (I have Vista Home Premium) and use the localhost. It worked! FF, IE, Chrome and Opera accessed the files the same way. So the lesson I learned is: you can use localhost to simulate the behavior of a server, which is useful when developing locally.

With FF not accessing the remote file, and IE being able to access it, I don't really get that...oh well for now my problem is solved, I will report back with future frustrations. Thanks for reading!

Troy