Solved

responseXML is always null on php script return

Posted on 2009-05-07
11
1,343 Views
Last Modified: 2013-12-13
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

0
Comment
Question by:JFercan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
  • +1
11 Comments
 
LVL 12

Accepted Solution

by:
alien109 earned 167 total points
ID: 24328012
Few questions- are you testing this on a remote server? Is the server set up with the correct mime-types for xml?
0
 
LVL 2

Author Comment

by:JFercan
ID: 24328042
Everything is running on my Dev machine (Vista Ultimate 32, IIS7)  I'll check IIS for the mime-types.

Thank you.
0
 
LVL 2

Author Comment

by:JFercan
ID: 24328100
Mime-Types seem OK .xml = text/xml  Is there any other place I should check?

Thank you.
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 4

Assisted Solution

by:aconrad
aconrad earned 167 total points
ID: 24328118
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
 
LVL 3

Assisted Solution

by:EverLearningCodeMonkey
EverLearningCodeMonkey earned 166 total points
ID: 24328151
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
 
LVL 12

Expert Comment

by:alien109
ID: 24328192
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
 
LVL 2

Author Comment

by:JFercan
ID: 24328243
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
 
LVL 12

Expert Comment

by:alien109
ID: 24328261
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
 
LVL 2

Author Comment

by:JFercan
ID: 24328266
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
 
LVL 2

Author Comment

by:JFercan
ID: 24328438
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
 
LVL 3

Expert Comment

by:EverLearningCodeMonkey
ID: 24328807
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

695 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question