doug.linley
02-10-2008, 02:04 AM
I'm just getting started with Ajax, and because of an issue that I have trying to get javascript to execute from innerHTML, I wanted to test using XML and parse it into the DOM.
I've looked at a few examples and it seemed easy enough, but I just can't seem to get it to work.
Here is what I'm doing.
I've got this simple PHP page returning some test xml. This would be pulling values from the database in production.
<?php
header('Content-Type: text/xml');
?>
<?xml version="1.0" encoding="utf-8"?>
<response>
<h3 id="test" class="success">This is a test.</h3>
<p id="normal" class="normal">blah blah blah blah blab blahblah blah blah blah blab blahblah blah blah blah blab blah</p>
</response>
Now I have the following javascript, just trying to get at the first node to try to get some data.
function parseXMLResponse(request, target) {
if(request.readyState == 4) {
if(request.status == 200 || request.status == 304) {
alert(request.responseText);
var data = request.responseXML;
alert(data.getElementsByTagName("response")[0].firstChild.data);
} else {
alert(request.responseText);
}
}
}
The error I'm receiving is: data.getElementsByTagName("response")[0] has no properties.
The alert on the responseText shows what appears to be a valid XML document, and you can see that I'm setting the mime-type to text/xml in the php. I'm sending the data using a post from a form because that's how this particular piece will work in production. Could that possibly mess with it? Here is the send code:
function sendData(data,script,target) {
var request = getAjaxObject();
if(request) {
request.onreadystatechange = function() {
parseXMLResponse(request,target);
};
request.open("POST", script, true);
request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
request.send(data);
return true;
} else {
return false;
}
}
I've also tried setting data to data = request.responseXML.documentElement; and I get the same issue. Can anyone give me a tip to what I may be doing wrong? I appreciate the feedback.
Thanks!
-Doug
I've looked at a few examples and it seemed easy enough, but I just can't seem to get it to work.
Here is what I'm doing.
I've got this simple PHP page returning some test xml. This would be pulling values from the database in production.
<?php
header('Content-Type: text/xml');
?>
<?xml version="1.0" encoding="utf-8"?>
<response>
<h3 id="test" class="success">This is a test.</h3>
<p id="normal" class="normal">blah blah blah blah blab blahblah blah blah blah blab blahblah blah blah blah blab blah</p>
</response>
Now I have the following javascript, just trying to get at the first node to try to get some data.
function parseXMLResponse(request, target) {
if(request.readyState == 4) {
if(request.status == 200 || request.status == 304) {
alert(request.responseText);
var data = request.responseXML;
alert(data.getElementsByTagName("response")[0].firstChild.data);
} else {
alert(request.responseText);
}
}
}
The error I'm receiving is: data.getElementsByTagName("response")[0] has no properties.
The alert on the responseText shows what appears to be a valid XML document, and you can see that I'm setting the mime-type to text/xml in the php. I'm sending the data using a post from a form because that's how this particular piece will work in production. Could that possibly mess with it? Here is the send code:
function sendData(data,script,target) {
var request = getAjaxObject();
if(request) {
request.onreadystatechange = function() {
parseXMLResponse(request,target);
};
request.open("POST", script, true);
request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
request.send(data);
return true;
} else {
return false;
}
}
I've also tried setting data to data = request.responseXML.documentElement; and I get the same issue. Can anyone give me a tip to what I may be doing wrong? I appreciate the feedback.
Thanks!
-Doug