[AJAX] [PROGRAMMING] The responseXML is null or has an error.
The steps to debug this situation!
The responseXML is null or has an error.
The steps to debug this situation!
I am not sure if it is the moon or the weather turning to fall, but there seem to be a lot of questions related to responseXML and "why am I not getting a value back when I use var xmlDoc = this.req.responseXML.documentElement?"
I have talked about this in the past: http://radio.javaranch.com/pascarello/2005/10/26/113034827699 6.html, but I thought I would put some more information in how to debug this error.
After answering the question a bunch of times in the past year, I found that the following questions will find the error 99% of the time:
- Did you set your content type to text/xml?
- Is your request actually making it to the server and back?
- When you alert/examine the responseText, do you see anything that does not belong?
- Open up the dynamic XML document in your browser directly. (Type the url into the browser. This may require you to hard code post parameters or build a test form.) The browser will tell you if your XML is not formatted correctly. Is it formatted correctly?
This is what the questions normally turn up:
- No real explanation needed, but it seems to be one of the main reasons why people have errors with returning an XML document! Add a content type to your dynamic XML document and you are good to go!
- Some people fat finger the address of the xml page and do not realize it. Server returns a can not find file page, a developer forgets to check for status code and they have a problem reading the XML document. (Tomorrow I will post a quick article on Firebug to show you how to test this easily!) Fix the URL and you are good to go!
- I have had people that have shown me the responseText returned from the server with HTML markup in their so-called XML document. Why would this happen? They used response.write() to a normal page thinking that it will just return the XML. Remove the bad HTML code and you are good to go!
- The number one cause of not being able to read an XML document from responseText is bad XML markup! People leave out the xml tag line, they have unescaped characters in the text, and so on. When the file is opened up directly in the browser, it normally points you directly to the error. IE is actually very good at it! Fix the bad markup and you are good to go!
Now hopefully this helps you find your error with reading the responseXML. If anyone else has some tips that I left out, feel free to add them to the comments.
Eric Pascarello
Coauthor of Ajax In Action
Moderator of HTML/JavaScript at www.JavaRanch.com
Author of: JavaScript: Your Visual Blueprint for building Dynamic Web Pages