JFercan
asked on
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.
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;
}
///////////////////////////////////////////////////////////////////////////////
?>
///////////////////////////////////////////////////////////////////////////////
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Mime-Types seem OK .xml = text/xml Is there any other place I should check?
Thank you.
Thank you.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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/
Also - a great tool for testing this stuff (with remote servers) is charles
http://www.charlesproxy.com/
ASKER
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.
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.
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.
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.
ASKER
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.
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.
ASKER
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.
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.
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
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
ASKER
Thank you.