Pregunta

Hago una solicitud AJAX a una cadena JSON y luego lo analice, quiero devolver este objeto JSON llamado "Encuesta" del evento onReadyStatechange a la variable "Jacu" para acceder a globalmente

HERES MI CÓDIGO:

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();
}

¿Fue útil?

Solución

Si desea que Encuesta sea accesible a nivel mundial, debe declararlo fuera de su función.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top