Domanda

Faccio una richiesta Ajax a una stringa JSON e poi analillo, voglio restituire questo oggetto JSON chiamato "Encuesta" dall'evento onReadyStateChange alla variabile "Jacu" per accedere a livello globale

ecco il mio codice:

window.onload= function(){
 enter code here`tabla = document.getElementById("pregunta");
 enter code here`jencu= ajax("GET","datos/encuesta.json",true,"lee")
 }

function ajax(metodo,url,bolean,que){
   var xhr;
   if(window.XMLHttpRequest){
      xhr = new XMLHttpRequest();
   }else{ 
       xhr=new ActiveXObject("Microsoft.XMLHTTP");
   }
   xhr.onreadystatechange=function(){

           if (xhr.readyState==4 && xhr.status==200){
              if(que == "lee"){
                encuesta=xhr.responseText;
                  encuesta = JSON.parse(encuesta)
               }
           }
    }

   xhr.open(metodo,url,true);
   xhr.send();
}
.

È stato utile?

Soluzione

Se si desidera che Encuesta sia accessibile a livello globale, dovresti dichiararlo al di fuori della tua funzione.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top