Solved

responseXML is always null on php script return

Posted on 2009-05-07
11
1,331 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
  • 5
  • 3
  • 2
  • +1
11 Comments
 
LVL 12

Accepted Solution

by:
alien109 earned 167 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Mime-Types seem OK .xml = text/xml  Is there any other place I should check?

Thank you.
0
 
LVL 4

Assisted Solution

by:aconrad
aconrad earned 167 total points
Comment Utility
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
Comment Utility
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 12

Expert Comment

by:alien109
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Wordpress syntax error 1 27
how to resize preview image 4 28
Paging Using PHP 7 31
Strip leading 0 from a var 3 6
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

771 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now