Sto imparando ajax e sto seguendo alcuni script, ho trascritto questo codice a mano e non riesco a trovare l'errore
Gli argomenti alla funzione vengono passati, ho provato con gli alert per vedere il funzionamento e il caricamento dei file txt funziona. Quello che non va è il passaggio del testo al div chiamato contenuto.
Ecco il codice:
codice:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="it" xml:lang="it">
<head>
<title>Primo esempio AJAX</title>
<script type="text/javascript">
/*<![CDATA[*/
function getXMLHttpRequest(){
try{
return new XMLHttpRequest()
}catch(e){}
try{
return new ActiveXObject('Msxml2.XMLHTTP')
}catch(e){}
try{
return new ActiveXObject('Microsoft.XMLHTTP')
}catch(e){}
}
function apri(argomento)
{
var xmlHttp = getXMLHttpRequest();
xmlHttp.open("POST",argomento,false);
xmlHttp.send(null);
if (xmlHttp.status==200 || xmlHttp.status==0)
{
var risultatoTxt = xmlHttp.responseText;
var elem = document.getElementById("contenuto");
alert(elem);
elem.innerHtml = risultatoTxt;
}
else
{
alert("Comunicazione fallita (codice: "+xmlHttp.status+"! Motivo: "+xmlHttp.statusText);
}
}
/*]]>*/
</script>
</head>
<body>
<form action=".">
<input type="radio" name="argomento" value="storia" onclick="apri('storia.txt')">Storia
<input type="radio" name="argomento" value="geografia" onclick="apri('geografia.txt')">Geografia
<input type="radio" name="argomento" value="italiano" onclick="apri('italiano.txt')">Italiano
</form>
<div id="contenuto">
</div>
</body>
</html>