PDA

View Full Version : [AJAX] problema con responseXML


vizzz
08-02-2008, 00:31
forse è l'ora tarda, ma non riesco a fare il parsing della risposta xml...posto un po di codice:

ajax.js
function makeRequest(url)
{
var httpRequest;

if (window.XMLHttpRequest) { // Mozilla, Safari, ...
httpRequest = new XMLHttpRequest();
if (httpRequest.overrideMimeType) {
httpRequest.overrideMimeType('text/xml');
// See note below about this line
}
}
else if (window.ActiveXObject) { // IE
try {
httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
try {
httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) {}
}
}

if (!httpRequest) {
alert('Giving up :( Cannot create an XMLHTTP instance');
return false;
}
httpRequest.onreadystatechange = function() { alertContents(httpRequest); };
httpRequest.open('GET', url, true);
httpRequest.send('');

}

function alertContents(httpRequest) {

if (httpRequest.readyState == 4) {
if (httpRequest.status == 200) {
var response = httpRequest.responseXML.documentElement;
var n = response.getElementsByTagName('from')[0].firstChild.nodeValue;
document.getElementById("returned_value").innerHTML = n;
} else {
alert('There was a problem with the request.');
}
}

}

ajax.html
<html>
<head>
<script type="text/javascript" src="ajax.js"></script>
</head>
<body>
<form action="">
<select name="prova" onchange="makeRequest('getdata.php')">
<option value="1">prova1</option>
<option value="2">prova2</option>
<option value="3">prova3</option>
</select>
</form>
<span id="returned_value"></span>
</body>
</html>

getdata.php
<?php
// XML ajax response

echo "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>
<note>
<to>me</to>
<from>you</from>
<heading>Reminder</heading>
<body>abcdefgh</body>
</note>";
?>


mi da un errore in:

linea 42 carattere 3
'null' è nullo o non è un oggetto

che sarebbe la riga "document.getElementById("returned_value").innerHTML = n;"

ma da errore qualsiasi cosa scriva al posto di questo.
non riesco proprio ad uscirne.

se provo ad usare responseText vedo correttamente l'xml che mi ritorna.

isAlreadyInUse
08-02-2008, 09:46
Ho provato e a me funge...

vizzz
08-02-2008, 09:52
Ho provato e a me funge...

trovato la cazzata...manca header('Content-type: text/xml'); in getdata.php

uhmmm ti funziona senza? interessante

isAlreadyInUse
08-02-2008, 09:58
Si senza, io ho una servlet che mi fa questo

String result = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>";
result += "<note>";
result += "<to>me</to>";
result += "<from>you</from>";
result += "<heading>Reminder</heading>";
result += "<body>abcdefgh</body>";
result += "</note>"

E la prova l'ho fatta su firefox

vizzz
08-02-2008, 10:04
ok grazie...ora posso procedere :)