Weird Thoughts From Eric's Head

Tags - Categories : All | AJAX | BUSINESS | PERSONAL | PROGRAMMING | BOOK REVIEW

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:

  1. Did you set your content type to text/xml?
  2. Is your request actually making it to the server and back?
  3. When you alert/examine the responseText, do you see anything that does not belong?
  4. 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:

  1. 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!
  2. 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!
  3. 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!
  4. 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


If I remember right responseXML is a stream, so that reading it in a debugger will empty it, making it seem if it's bad. Be careful and let it be assigned to something first and then examine the assigned variable instead.
Thanks eric, I was having a problem with my ajax code working in firefox but not in internet explorer. When I set the response type to text/xml it worked!
Can you show a example about this: " Is your request actually making it to the server and back?" I think is my problem!
Just similar to Anonymous's post. Btw, Opera did xml job pretty well without text/xml http header. Thanks, Eric!
xml encoding is wrong and response can not get the corrent reply
Hey, thanks for the help! My fault was with the content type (but since I couldn't change the type of the .xml file, I used Gxml.parse(request.responseText) instead to get it into DOM format
I was testing some Ajax using static HTML files, and couldn't get responseXML to work (even when setting the meta content-type to text/xml) until I actually renamed the .html file to .xml
IE sucks at this. Firefox, safari, all pull up my xml document just fine, IE has random characters at the beginning of the document, I have gone through the whole document made sure the <?xml line is the very first character in the file, still no love.
hello sir,i'm a new guy.i have a question : i don't know how to use the type of dataset data on client with xmlhttp? this is my code: function getDataSource() { xmlhttp = createXmlhttp(); var url = "http://localhost/AjaxSample/HtmlWebServices/ws.asmx?op=ds_UserList"; xmlhttp.open("GET",url,true); xmlhttp.SetRequestHeader ("Content-Type","text/xml; charset=utf-8"); xmlhttp.SetRequestHeader ("SOAPAction","http://localhost/AjaxSample/HtmlWebServices/ds_UserList"); xmlhttp.onreadystatechange = HtmlBind; xmlhttp.send(null); return ; } function HtmlBind() { if(xmlhttp.readyState == 4) { if(xmlhttp.status == 200) { try { var tmp = xmlhttp.responseText; //alert(tmp); document.getElementById("div_Content").innerHTML = xmlhttp.responseXML.documentElement; } catch(ex) { err=true; } } else { alert("errcode:"+xmlhttp.status); } } } the "ds_UserList" is webservice method. return type of dataset data.
Another problem that can screw up your XML response when it is generated by PHP code is when you make a call to a PHP function that reports a warning such as the mail function does when there is a problem with the mail set up. You can suppress these warnings by prefixing the function call with '@'.


Add a comment

Title
Body
HTML : b, i, blockquote, br, p, pre, a href="", ul, ol, li
Math Quiz 6 + 4 = (Helps stop blog spam)
Name
E-mail address
Website
Remember me Yes  No 

E-mail addresses are not publicly displayed, so please only leave your e-mail address if you would like to be notified when new comments are added to this blog entry (you can opt-out later).

TrackBack to http://radio.javaranch.com/pascarello/addTrackBack.action?entry=1158096122600