salve,
apro un nuovo thread nonostante abbia gia accennato alla questione in un altro perchè il problema è cambiato ora e mi sembrava più giusto aprire una discussione dedicata.
vengo al dunque.
ho scritto la classe ajax che posto più sotto ed ho qualche difficoltà.
nel momento in cui ho ristrutturato il codice seguendo il formalismo ad oggetti mi è sorto questo problema con l'oggetto XMLHttpRequest ed in particolare con la proprietà readyState.
il fatto è che il codice non riesce ad entrare nel ramo IF la cui condizione è "if(this.objSOCK.readyState == 4)"...
non riesco a capire esattamente dove sia il problema...
ho provato a stampare degli alert per vederci più chiaro ma non hanno fatto che incasinarmi...
del tipo:
codice:
this.objSOCK.onreadystatechange=function(){alert(this.objSOCK.readyState);};
non apre nessun alert...
invece
codice:
this.objSOCK.onreadystatechange=function(){alert('pippo');};
apre 4 alert pippo (che dovrebbero essere, in teoria, i readyState 0 -1 - 2 - 4).
proprio non capisco...
se all'interno della funzione requestPage stampo i readyState dopo ogni operazione sull'oggetto xmlhttprequest vedo gli state 0, 1 e anche 2... se invece li stampo dalla funzione receiveResponse (quella richiamata dopo ogni onreadystatechange) esce sempre e solo "0"..
ed è per questo motivo che non nentra nel ramo IF...
ci sto diventando matto...
potete darmi una mano?
grazie
ecco il codice:
codice:
function ajax(){
this.objSOCK = null;
this.openSock = function(){
var xmlhttp ;
try{
xmlhttp = new XMLHttpRequest(); // Gecko (Firefox, Moz), KHTML (Konqueror, Safari), Opera, Internet Explorer 7
}catch(e){
var MSXML_XMLHTTP_PROGIDS = new Array(
'MSXML2.XMLHTTP.5.0',
'MSXML2.XMLHTTP.4.0',
'MSXML2.XMLHTTP.3.0',
'MSXML2.XMLHTTP', // Internet Explorer 6
'Microsoft.XMLHTTP' // Internet Explorer 4,5
);
var success=false;
for(var i=0;i < MSXML_XMLHTTP_PROGIDS.length && !success; i++){
try{
xmlhttp = new ActiveXObject(MSXML_XMLHTTP_PROGIDS[i]);
success=true;
}catch(e){}
}
if(!success)
alert('attenzione: per navigare questo sito e necessario abilitare javascript o activeX');
}
return xmlhttp;
}
this.printOut = function(target, html){
document.getElementById(target).innerHTML = html
}
this.receiveResponse = function(target) {
alert(this.objSOCK.readyState);
if(this.objSOCK.readyState == 4) {
if(this.objSOCK.status == 200 || this.objSOCK.statusText=="Found") {
this.printOut(target, this.objSOCK.responseText);
}else{
this.printOut(target, "error:\n"+objSOCK.statusText);
}
}
}
this.requestPage = function(url, target, method, parameters) {
if(method == undefined){method="GET";}
this.objSOCK=this.openSock(); // creo l'oggetto XMLHttpRequest
if(this.objSOCK){
//this.objSOCK.onreadystatechange=function(){alert(this.objSOCK.readyState);};
this.objSOCK.onreadystatechange=this.receiveResponse(target);
this.objSOCK.open(method, url, true);
if(method == "GET" || method == "get"){
this.objSOCK.send("");
}else{
this.objSOCK.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
this.objSOCK.setRequestHeader('Content-length', parameters.length);
this.objSOCK.setRequestHeader('Connection', 'close');
this.objSOCK.send(parameters);
}
}
}
}