Ajax - Parsing XML with JavaScript
Posted: Sat Aug 20, 2005 4:19 pm
Hello,
I just played around with some Ajax examples, but have problems parsing XML data with JavaScript.
The XML file sent by the PHP script looks like this:
The header is set correctly:
The relevant part of the JavaScript code is here:
That's the error message from the JavaScript console in Opera:
If I replace the line
with
it spits out the XML data correctly. I am using Apache 1.3.33 and PHP 5.0.4. I tested the example with Opera 8.0.2 and Firefox 1.0.6 on Slackware Linux 10.1. I have no idea what the problem is. The use of the DOM functions looks correct for me. Thanks.
Lars
I just played around with some Ajax examples, but have problems parsing XML data with JavaScript.
The XML file sent by the PHP script looks like this:
Code: Select all
<?xml version="1.0" encoding="ISO-8859-1" standalone="true" ?>
<update>
<id>element</id>
<content>some content...</content>
</update>Code: Select all
header("Content-Type: text/xml");
header("Vary: Accept");Code: Select all
function handleResponse() {
if (reqObj.readyState == 4) {
if (reqObj.status == 200) {
var response = reqObj.responseXML.documentElement;
var id = response.getElementsByTagName("id").item(0).firstChild.data;
var content = response.getElementsByTagName("content").item(0).firstChild.data;
document.getElementById(id).innerHTML = content;
}
}
}Code: Select all
AJAX
http://localhost/sandbox/AJAX/
Unknown thread
Error:
name: TypeError
message: Statement on line 57: Could not convert undefined or null to object
Backtrace:
Line 57 of inline#1 script in http://localhost/sandbox/AJAX/
var id = (response.getElementsByTagName("id").item(0)).firstChild.data;
At unknown location
[statement source code not available]Code: Select all
var response = reqObj.responseXML.documentElement;Code: Select all
var response = reqObj.responseHTML;
alert(response);Lars