responseXML is always null on php script return

I've been fighting with this for a while now.  I have searched the web and still could not find a solution to my problem.

I have JavaScript that calls a server PHP script synchronously using the XMLHttpRequest object.  I'm returning a simple XML document from the server, but the responseXML property is always null and the Content-Type header is always text/html.  Please take a look at my code and let me know what it is I'm doing worng.

I tested the PHP script on both FireFox and IE and they display the XML document and using FireFox web developer add-on I can see that the header is set to text/xml.

Thank you in advance for your help.
///////////////////////////////////////////////////////////////////////////////
JavaScript:
var xmlhr = false;
if(typeof XMLHttpRequest != "undefined")
	xmlhr = new XMLHttpRequest();
else if(typeof ActiveXObject != "undefined"){
	try{
		xmlhr = new ActiveXObject("MSXML2.XMLHTTP.3.0");
	}
	catch(e){
		alert("Error creating XMLHttpRequest: " + (e.description ? e.description : e.message));
	}
}
 
if(xmlhr){
		// Open the connection to the server
		xmlhr.open("POST", script.php, false);
		xmlhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
		xmlhr.send(postdata);
		if(xmlhr.status == HttpStatus.OK){
			// This always displays "text/html"
			alert(xmlhr.getResponseHeader("Content-Type"));
			var value = xmlhr.responseXML;
			// This always displays null
			alert(value);
		}
}
///////////////////////////////////////////////////////////////////////////////
PHP Script:
<?php
header('Content-Type: text/xml');
header("Cache-Control: no-cache, must-revalidate");
echo(DisplayError("There was an error connecting to the server \"No Function Selected\".  Please try agin later."));
 
///////////////////////////////////////////////////////////////////////////////
// This function will return the specified error message to the caller
//
function DisplayError($message)
{
	$xmlText = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
	$xmlText .= "\n<response>";
	$xmlText .= "\n\t<error>$message</error>";
	$xmlText .= "\n</response>";
	
	return $xmlText;
}
///////////////////////////////////////////////////////////////////////////////
?>
///////////////////////////////////////////////////////////////////////////////

Open in new window

LVL 2
JFercanAsked:
Who is Participating?
 
alien109Connect With a Mentor Commented:
Few questions- are you testing this on a remote server? Is the server set up with the correct mime-types for xml?
0
 
JFercanAuthor Commented:
Everything is running on my Dev machine (Vista Ultimate 32, IIS7)  I'll check IIS for the mime-types.

Thank you.
0
 
JFercanAuthor Commented:
Mime-Types seem OK .xml = text/xml  Is there any other place I should check?

Thank you.
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
aconradConnect With a Mentor Commented:
the content type is text/html because you set it text/html at line 31 in your example.

i changed a bit your example, made into a single php file and
i get an object XMLDocument instead of null

<?php
//this file is name exp1.php
 
function DisplayError($message)
{
	$xmlText = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
	$xmlText .= "\n<response>";
	$xmlText .= "\n\t<error>$message</error>";
	$xmlText .= "\n</response>";
 
	echo $xmlText;
}
 
 
if (isset($_GET['ajax']))
{
 
	header('Content-Type: text/xml');
	header("Cache-Control: no-cache, must-revalidate");
 
	DisplayError("There was an error connecting to the server \"No Function Selected\".  Please try agin later.");
 
	///////////////////////////////////////////////////////////////////////////////
	// This function will return the specified error message to the caller
	//
 
	///////////////////////////////////////////////////////////////////////////////
 
	die();
}
 
?>
 
<script>
var postdata = 'qwe=asd';
var xmlhr = false;
 
if(typeof XMLHttpRequest != "undefined")
xmlhr = new XMLHttpRequest();
else if(typeof ActiveXObject != "undefined"){
	try{
		xmlhr = new ActiveXObject("MSXML2.XMLHTTP.3.0");
	}
	catch(e){
		alert("Error creating XMLHttpRequest: " + (e.description ? e.description : e.message));
	}
}
 
if(xmlhr){
	// Open the connection to the server
 
	xmlhr.open("POST", 'exp1.php?ajax=1', false);
	xmlhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
	xmlhr.send(postdata);
 
	if(xmlhr.status == '200'){
		// This always displays "text/html"
		alert(xmlhr.getResponseHeader("Content-Type"));
		var value = xmlhr.responseXML;
		// This always displays null
		alert(value);
	}
}
 
</script>

Open in new window

0
 
EverLearningCodeMonkeyConnect With a Mentor Commented:
Hi JFercan,

I'm curious if responseText is also empty

You might want to try outputting that as well to see if you're getting anything in your response at all.

Also, you might want to look into using a JavaScript logger to help you see what's going on inside your code.  It's a nice way to get away from using a bunch of alert() calls, makes JavaScript debugging far less annoying.  I've been using Blackbird lately and found it pretty helpful.

http://www.gscottolson.com/blackbirdjs/
0
 
alien109Commented:
What's the URL you are using to test locally?

Also - a great tool for testing this stuff (with remote servers) is charles
http://www.charlesproxy.com/
0
 
JFercanAuthor Commented:
EverLearningCodeMonkey:
responseText does return the XML document.  I'll check out the debugger  Thank you.


aconrad:
At line 31 I did set it to text/xml not text/html, which is what is requred.  When I display the headers after calling the php script by itself in Firefox I see that Content-Type is set to text/xml and the document validates correctly.  But on my XMLHttpRequest object the header get's reset to text/html for some reason.  I switched the code around to have the function on top, but I still receive the same error.
Thank you.
0
 
alien109Commented:
Just for giggles and grins - What happens if you put an extra period after the ip/hostname and port?

http://localhost./
http://localhost.:<port>/
http://127.0.0.1./
http://127.0.0.1.:<port>/

I've had this issue in the past with IE not returning responseXML when testing locally. However the issue didn't occur in Firefox.
0
 
JFercanAuthor Commented:
alien109:
http://localhost/index.html is the page and inside I just call the script as in the code as the php is in the same directory.  I'll check charles out.  Thank you.
0
 
JFercanAuthor Commented:
OK.  All is working OK now and I feel like an idiot.

The java script calls a local php script that calls en external php script (which right now i only simulate with a different directory).

I was setting the content-type on the external script, but not on the local script, therefore the script that was returning the content to the javascript was still sending html instead of xml.  I'm really sorry for waisting your time, I will split the points among the three of you for participating.

EverLearningCodeMonkey:  That debugger looks very cool.
0
 
EverLearningCodeMonkeyCommented:
No problem,

The smallest of mistakes always seem to be the ones that cause the most infuriating errors.  Occupational hazard I guess.

Yeah, I've been pretty impressed with Blackbird, makes JS development a little less painful, and that's always a good thing.

Take care,

ELCM
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.