2

我向 json 字符串发出 ajax 请求,然后解析它,我想将这个名为“encuesta”的 Json 对象从事件 onreadystatechange 返回到“jacu”变量以全局访问

这是我的代码:

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

1 回答 1

0

如果您希望 encuesta 可以全局访问,您应该在您的函数之外声明它。

于 2014-02-25T20:35:54.353 回答