Salve a tutti, ho un problema nella gestione dei dati in XML.
Il codice è questo:
index.html
Codice PHP:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="it" xml:lang="it">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Sito Ecommerce</title>
<link rel="stylesheet" type="text/css" href="stile.css" />
<script type="text/javascript" src="ajax.js"></script>
</head>
<body onload="init();">
<div id="messaggio"></div>
</body>
</html>
Il file javascript in ajax è questo:
Codice PHP:
function createXMLHttpRequest() {
var XHR = null, browserClient = navigator.userAgent.toUpperCase(); if(typeof(XMLHttpRequest) === "function" || typeof(XMLHttpRequest) === "object") XHR = new XMLHttpRequest();
else if(windows.ActiveXObject && browserClient.indexOf("MSIE 4") < 0) { if(browserClient.indexOf("MSIE 5") < 0)
XHR = new ActiveXObject("Msxml2.XMLHTTP");
else
XHR = new ActiveXObject("Microsoft.XMLHTTP"); }
return XHR; }
function init() {
var http = createXMLHttpRequest();
var dataChiamata = new Date(),
inizioChiamata = dataChiamata.getTime(),
massimaAttesa = 5,
verificaTempoTrascorso;
if(http) {
http.open("post", "processdata.php", true);
http.setRequestHeader("content-type", "application/x-www-form-urlencoded"); /*http.setRequestHeader("connection", "close");*/
http.onreadystatechange = function() {
if (http.readyState == 4) {
verificaTempoTrascorso = function(){};
if (http.status == 200) {
var responso = http.responseXML;
var messaggio = document.getElementById("messaggio");
if(responso) {
messaggio.innerHTML = "<h3> La richiesta asincrona in XML è avvenuta con successo</h3>";
} else {
alert('Errore nella gestione dei dati: ' + http.responseText); }
}
} else if(massimaAttesa < 1000) {
massimaAttesa = massimaAttesa * 1000;
verificaTempoTrascorso = function() {
dataChiamata = new Date();
if((dataChiamata.getTime() - inizioChiamata) > massimaAttesa) { ajax.onreadystatechange = function(){return;};
ajax.abort();
ajax = document.createElement("p");
ajax.innerHTML = "Spiacente, richiesta fallita.
" +
"La prego di ritentare tra qualche istante."; elemento.appendChild(ajax);
} else
setTimeout(verificaTempoTrascorso, 100); };
verificaTempoTrascorso();
}
}
http.send(null); } }
e infine il file processdata.php da cui prendo i dati:
Codice PHP:
<?php require_once('config.php');
require_once('xmlwriter.php');
$query = mysql_query("SELECT * FROM $_CONFIG[articoli]");
$xml = new XMLWriter2(); $xml->push('articoli');
while($result = mysql_fetch_assoc($query)) {
$xml->push('articolo'); $xml->element('codice', $result['codice']); $xml->element('descrizione', $result['descrizione']);
$xml->element('misura', $result['misura']);
$xml->element('categoria', $result['categoria']);
$xml->element('prezzo', $result['prezzo']);
$xml->element('smallfoto', $result['smallfoto']);
$xml->element('bigfoto', $result['bigfoto']);
$xml->element('descest', $result['descest']);
$xml->pop('articolo');
}
$xml->pop();
print $xml->getXml();
?>
Il problema è questo, ogni volta che faccio una richiesta asincrona, il server risponde correttamente con un file xml ben formato e fornito di tutti i dati del database ma in javascript responseXML sembra risultare nullo.
Capisco che nessuno voglia sbattersi a comprendere il codice ma mi basta anche sapere da dove cominciare a vedere per risolvere il problema, grazie.