Ciao a tutti
sto creando una funzione(madre) javascript che all'interno va a richiamare un'altra funzione(ajax) che manda i dati ad una pagina asp e la risposta mi serve all'interno poi della funzione madre
per richiamare la funzione ajax scrivo così (nella funzione madre)
codice:
prendoDato=sendRequest(url,handleRequest);
alert(" prendoDato " + prendoDato)
mentre la funzione ajax è questa
codice:
function handleRequest(req) {
//var writeroot = [some element];
//writeroot.innerHTML = req.responseText;
// alert("req " +req)
}
function sendRequest(url,callback,postData) {
var req = createXMLHTTPObject();
if (!req) return;
var method = (postData) ? "POST" : "GET";
req.open(method,url,true);
// req.setRequestHeader('User-Agent','XMLHTTP/1.0');
req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
if (postData)
req.setRequestHeader('Content-type','application/x-www-form-urlencoded');
req.onreadystatechange = function () {
alert('HTTP error ' + req.status);
if (req.readyState != 4) return;
if (req.status != 200 && req.status != 304) {
// alert('HTTP error ' + req.status);
return;
}
if (req.status == 200)
{
alert("req " +req.responseText)
return req.responseText
}
callback(req);
}
if (req.readyState == 4) return;
req.send(postData);
}
var XMLHttpFactories = [
function () {return new XMLHttpRequest()},
function () {return new ActiveXObject("Msxml2.XMLHTTP")},
function () {return new ActiveXObject("Msxml3.XMLHTTP")},
function () {return new ActiveXObject("Microsoft.XMLHTTP")}
];
function createXMLHTTPObject() {
var xmlhttp = false;
for (var i=0;i<XMLHttpFactories.length;i++) {
try {
xmlhttp = XMLHttpFactories[i]();
}
catch (e) {
continue;
}
break;
}
return xmlhttp;
}
il problema che ho è questo io richiamo la funzione ajax
e vorrei mettere il valore di ritorno in una variabile nella funzione madre
il problema e che l'ajax funzione ma passa il valore undefined alla variabile
per verificare ho messo degli alert e praticamente
mi esce prima l'alert che si trova dopo la chiamata alla funzione ajax
e poi gli alert che sono nella funzione ajax, che dopo intercetta il valore, ma che non riesce più a passarlo alla variabile prendoDato
ho provato nella funzione madre a mettere un ciclo
ma mi va in loop eterno
codice:
do {
cccc=sendRequest(url,handleRequest);
alert("cccc "+cccc)
}
while (cccc !== "undefined");
avete qualche suggerimento da darmi?
Grazie mille in anticipo