我对JSON字符串进行Ajax请求,然后我解析它,我想将这个名为“Encuesta”的JSON对象从事件onreadyStateChange返回到“JACU”变量以访问全局

heres我的代码:

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

有帮助吗?

解决方案

如果您希望全局可访问Encuesta,则应在您的功能之外声明。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top