从Ajax的事件onreadyStateChange返回JSON对象
-
21-12-2019 - |
题
我对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,则应在您的功能之外声明。
不隶属于 StackOverflow