Salve, vorrei integrare sul mio progetto una funzione che mi avvertisse quando un connessione ajax è terminata. Questo link http://javascript.html.it/articoli/l...ax-e-jquery/5/ spiega come gestire al meglio una connessione ajax-jquery.
Questo e il mio script:
- //globals
var first = ""; //id of first SELECT
var second = ""; //id of second SELECT
var HttpMethod="POST";
function clicked(sql,topic,subcats) {
first = topic;
second = subcats;
stringa = "topic=";
var el = document.getElementById(first);
var ob2=document.getElementById(second);
var selected = el.selectedIndex;
while(ob2.hasChildNodes()) { //removes items from dropdown if some already exist
ob2.removeChild(ob2.firstChild);
}
if(selected!= 0) { //if they choose something other than the first select-->"Select topic first"
stringa += el.value;
AJAXReq("POST","fetch.php,true);
ob2.disabled=0;
} else { //otherwise add the Select Topic First option and disable it
var childEl = document.createElement('option');
ob2.appendChild(childEl);
childEl.innerHTML = 'Select Topic First';
ob2.disabled=1;
}
}
function handleResponse(){
if(myReq.readyState == 4){
if(myReq.status == 200){
data=myReq.responseText;
var items = data.split(',');
var length = items.length;
for(var i = 0; i < length; i++) {
var childEl = document.createElement('option'); //create option
var El = document.getElementById(second);
El.appendChild(childEl); //then append it to the second dropdown list
childEl.value = items[i]
childEl.innerHTML = items[i];
}
}else{
/* alert("Niente da fare, AJAX non funziona"); */
}
}
}
function AJAXReq(method,url,bool){
// alert("ajax funz ");
if(window.XMLHttpRequest){
myReq = new XMLHttpRequest();
} else
if(window.ActiveXObject){
myReq = new ActiveXObject("Microsoft.XMLHTTP");
if(!myReq){
myReq = new ActiveXObject("Msxml2.XMLHTTP");
}
}
if(myReq){
execfunc(method,url,bool);
}else{
/* alert("Impossibilitati ad usare AJAX"); */
}
}
function execfunc(method,url,bool){
myReq.onreadystatechange = handleResponse;
myReq.open(method,url,bool);
myReq.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
myReq.send(stringa);
//myReq.send('topic=004');
}
:master: