Ciao a tutti:
sto implementando una funzione AJAX piuttosto semplice e basilare: ho un form piuttosto grande, e un suo relativo file asp di aggiornamento dei dati nel database: l'idea è di inserire un autosave (come quello di excel o word) che, ad intervalli regolari, chiami in background il file di aggiornamento e salvi i dati nel database.
Cercando anche degli script in rete ho messo assieme una serie di funzioni che, se eseguite in firefox (3 e ora anche 4), funzionano a meraviglia; se eseguite in IE8 ottengo dei comportamenti strani: in alcuni, rarissimi casi, fa l'aggiornamento (mai più di una volta per sessione, comunque, e se lo fa è alla prima chiamata), per il resto, appena il ReadyState arriva a 4, ottengo come status un surreale 12015 che, cercando in rete (e non senza fatica) ho reperito come un errore di "Login failure". Ma login a cosa???
Il codice ajax è questo:
codice:
<script type="text/javascript">
function getHTTPObject() {
var http = false;
if(typeof ActiveXObject != 'undefined') {
try {http = new ActiveXObject("Msxml2.XMLHTTP");}
catch (e) {
try {http = new ActiveXObject("Microsoft.XMLHTTP");}
catch (E) {http = false;}
}
} else if (XMLHttpRequest) {
try {http = new XMLHttpRequest();}
catch (e) {http = false;}
}
return http;
}
var oXHR = getHTTPObject();
function starttimer()
{
t = setTimeout("AutoSave()", 10000);
}
function RetrieveArguments()
{
var form = document.getElementById('Form<%=paginacorrente%>');
var tosend = ""
for(var i=0;i<form.length;i++)
{
if(form.elements[i].type=='radio' || form.elements[i].type=='checkbox')
{
if(form.elements[i].checked==true)
{
tosend = tosend + form.elements[i].name + '=' + form.elements[i].value + '&';
}
}
else
{
tosend = tosend + form.elements[i].name + '=' + form.elements[i].value + '&';
}
}
tosend = tosend + 'TipoInvio=0';
return tosend;
}
function Miohandler()
{
if(oXHR.readyState == 4)
{
switch (oXHR.status)
{
case 200:
document.getElementById('StatoSalvataggio').innerHTML = "Ultimo salvataggio effettuato il " + getCalendarDate() + " " + getClockTime();
break;
default:
document.getElementById('StatoSalvataggio').innerHTML = "Errore nel salvataggio automatio (codice=" + oXHR.status + ")";
break;
}
starttimer();
}
else
{
document.getElementById('StatoSalvataggio').innerHTML = "Salvataggio in corso..."
}
}
function AutoSave()
{
var parameters = RetrieveArguments();
oXHR.open("POST", "<%=Application("rootdir")%>Agg<%=paginacorrente%>.asp", true)
oXHR.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
oXHR.setRequestHeader("Content-length", parameters.length);
oXHR.setRequestHeader("Connection", "close");
oXHR.onreadystatechange = Miohandler;
oXHR.send(parameters);
}
</script>
Il tutto viene avviato onload con una chiamata a starttimer()
Non capisco cosa non piaccia a IE... a qualcuno è mai capitata una cosa simile?