Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

XMLHttpRequest.responseXML, What Am I Doing Wrong?

 
L Lindstrom
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Folks:

I'm learning JavaScript and XML.

I'm having trouble getting XMLHttpRequest.responseXML to work for me.

This is simplified from a sample from W3Schools. I couldn't get the sample working either.

These HTML and XML files are on at my site, you can run it, and perhaps see what I'm doing wrong, at:

http://larry.us.com/learn/javascript/XMLHttpRequest/responseXML.html

Both HTML and XML are also attached. Files with extensions of "HTML" and "XML" can't be attached. So extensions have been dropped.

The following statement seems to work:

xmlhttp=new XMLHttpRequest();

And this seems to work:

xmlhttp.open("GET","note.xml",false);
xmlhttp.send();

Because this seems to work:

document.getElementById("response").innerHTML = xmlhttp.response;

The text from the file "note.xml" is applied to the paragraph with an ID of "response".

So far, so good. This XMLHttpRequest is a cool feature.

But now it falls appart. This statement doesn't seem to work:

xmlDoc=xmlhttp.responseXML;

xmlDoc is NULL.

I've tried several different XML files, taken from different sites on the web, and responseXML seems to be NULL for those also.

What am I doing wrong?

One more question.

No "var" is defined for the left hand part of "xmlhttp=new XMLHttpRequest();" or "xmlDoc=xmlhttp.responseXML;". Aren't these JavaScript variables?

Thanks
Larry

Sorry. I've been unable to attach my HTML and XML files to this post. I keep getting messages that files in attachment can't have extensions of "XML", "HTML", "EX-EM-EL", "ACH-TEE-EM-EL" or any other variant I've tried. I've tried just attaching the file "Note" and received this error message "Files with the extension .note are not allowed as attachment in the message.".

The name of the XML file I attempted to attach was "note". No extension.

So here is the XML file that's being read:


I'll attempt to attach the HTML, without an extension.

Nope! "Files with the extension .responsexml are not allowed as attachment in the message."

The name of the HTML file I attempted to attach was "responseXML". No extension.


Thanks
Larry

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65223
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does your response have the right content type identifying the response as XML?

What does xmlhttp.responseText contain?

If responseText has the XML as a string, an incorrect content type is the likely cause.
 
L Lindstrom
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Bear:

My mistake, something's wrong with my Apache setup.

Testing this on my server, I'm getting this error:

Error Cannot call method 'getElementsByTagName' of null

This is caused by XMLHttpRequest.responseXML returning NULL.

When I ran this on my site, hosted by someone who has a clue, the error was

Error Cannot set property 'innerHTML' of null

I wasn't looking at them side by side, and assumed it was the same error message.

It wasn't the same error. The hosted site was returning a valid XMLHttpRequest.responseXML. The error was later, when I tried:

document.getElementById("to").innerHTML = ...

I have no HTML tag with an ID of "to".

Sorry.

I appreciate your response.

Larry


 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65223
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No problem. Post back if you have further problems.
 
L Lindstrom
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Again Bear:

I'm a C++ programmer.

A couple of more questions:

What's with the variables "xmlhttp" and "xmlDoc" in the code sample?

They aren't declared with "var" statements. Are var statements required?

xmlhttp seems to be created in an if's condition braces. But it's used outside the braces. Any scoping issues?

Thanks
Larry
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65223
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
L Lindstrom wrote:
What's with the variables "xmlhttp" and "xmlDoc" in the code sample?

They aren't declared with "var" statements. Are var statements required?


The var is optional, and if omitted will create the variable in the global scope (the window object). It is considered sloppy and a poor practice to omit the var.

xmlhttp seems to be created in an if's condition braces. But it's used outside the braces. Any scoping issues?

Yes. It should be declared at the top level and set in the inner scope. That makes it explicitly clear what the scope of the variable is, rather then relying on the implicit global behavior of omitting var.

JavaScript is very different from C++, and even from Java (despite its name), and it may take some work to grok its behavior.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic