全局变量AJAX

时间:2016-12-13 05:10:15

标签: javascript ajax

来自begginer one的问题。 通过AJAX函数,我从我的脚本中获取JSON数组。在示例中,我想在全局变量中保存responseText(数组),因为我需要在另一个函数中使用它。我不能。我希望有办法做到这一点,但我找不到一个。

var out;
var arr; //global variable

function ajax(kat1) {
if (kat1 == "") {
    document.getElementById("div1").innerHTML = "";
    return;
} else { 
    if (window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest();
    } else {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {

            arr=JSON.parse(this.responseText); //here might be the problem
        }
    }
    xmlhttp.open("GET","script.php?q="+kat1,true);
    xmlhttp.send();
 }
}




function show(response, i)
{
var arr1 = arr;
out = "Question"+arr1[i].id+": "+arr1[i].question+"? ";
document.getElementById("quizblock10").innerHTML = out;
}

0 个答案:

没有答案
相关问题