Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

responseXML null in FF, object in IE; responseText works in both

Status
Not open for further replies.

mikelg

Technical User
Apr 22, 2008
2
US
Hello,

I've used pretty much this same code in projects before with no problem, however, this time around, I can't get a status=200. I've alerted several things, and the responseXML comes back null in FF (2/3), object in IE (6/7), while the responseText comes through just fine in both.

I've tested it both locally and on a web server. I've been searching for answers on this, and it points to it not being recognized as XML, even though it passed a validator.

XHR part:
Code:
<script type="text/javascript">
	var xmlHttp;
	var requestType = "";
	var images = null;
	var alts = null;
	var links = null;
	var titles = null;			
	var currentPromo = null;	// Helps build part of the block
	var currentItem = 1;		// Global variable that keeps track of the rotating items
	
	function createXMLHttpRequest() {
		if (window.ActiveXObject) {
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		else if (window.XMLHttpRequest) {
			xmlHttp = new XMLHttpRequest();
			xmlHttp.overrideMimeType('html/xml')
		}
	}
	
	function startRequest() {
		createXMLHttpRequest();
		xmlHttp.onreadystatechange = handleStateChange;
		xmlHttp.open("GET", "homeblock.xml", true);
		xmlHttp.send(null);
		alert(xmlHttp.responseXML);	
		alert(xmlHttp.responseText);		
	}
	
	function handleStateChange() {
		if(xmlHttp.readyState == 4) {
		alert(xmlHttp.readyState);	
				if(xmlHttp.status == 200) {
				listBlockItems();
		alert(xmlHttp.status);	
			}
		}
	}
=============================================
XML File
=============================================
Code:
<?xml version="1.0" encoding="utf-8"?>
	<rss version="2.0">
		<channel>
			<title>Feed title</title>
			<link>[URL unfurl="true"]http://www.google.com</link>[/URL]
			<description>Description goes here</description>
			<item>
				<title>Item #1</title>
				<link>[URL unfurl="true"]http://www.google.com</link>[/URL]
				<description>Description #1</description>
				<image>slide1.jpg</image>
				<alt>Item #1 Image</alt>
			</item>
			<item>
				<title>Item #2</title>
				<link>[URL unfurl="true"]http://www.google.com</link>[/URL]
				<description>Description #2</description>
				<image>slide2.jpg</image>
				<alt>Item #2 Image</alt>
			</item>
			<item>
				<title>Item #3</title>
				<link>[URL unfurl="true"]http://www.google.com</link>[/URL]
				<description>Description #3</description>
				<image>images/slide3.jpg</image>
				<alt>Item #3 Image</alt>
			</item>
			<item>
				<title>Item #4</title>
				<link>[URL unfurl="true"]http://www.google.com</link>[/URL]
				<description>Description #4</description>
				<image>slide4.jpg</image>
				<alt>Item #4 Image</alt>
			</item>
	</channel>
	</rss>
====================================
Any help is appreciated, and insults probably deserved :)
 
Thanks for your reply.

I realized I hadn't removed
Code:
xmlHttp.overrideMimeType('html/xml')
yet (it was a suggestion from a blog post), which didn't work in this case anyway. My apologies.

I tried a couple of different ways to set the header, among them:
Code:
		xmlHttp.setRequestHeader('Content-Type', 'text/xml');
as text/xml, but have still had no luck.

I also tried a pretty detailed dtd to make sure the xml was well-formed, and it passed an online validator, but it still returned null as responseXML, but passed the reponseText.

I can get to readyState 4, it just never reaches OK status.
 
I can tell you this much: because your AJAX call is asynchronous, responseXML (in your example above) will come back null 100% of the time since the request hasn't had time to complete by the time you call alert(responseXML). However, since the alert box has to pop on the screen, this gives your script that little bit more time to complete the AJAX request, and hence why you have data populated in responseText.

I copied your code exactly, and modified the handleStateChange() slightly:
Code:
function handleStateChange() {
  if(xmlHttp.readyState == 4) {
    // request complete - display property values
    alert(xmlHttp.readyState);    
    alert(xmlHttp.status);   
    alert(xmlHttp.responseXML);    
    alert(xmlHttp.responseText);                   
  }
}
to which I received an XML object in responseXML, and the XML file contents in responseText.

Let me know if this works, or if you already found a solution.

Greg
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top