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