Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/420.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在javascript responseText中将临时变量声明为全局变量_Javascript_Variables - Fatal编程技术网

在javascript responseText中将临时变量声明为全局变量

在javascript responseText中将临时变量声明为全局变量,javascript,variables,Javascript,Variables,我试图将xhr.responseText变成一个变量,然后提醒它,但由于一个奇怪的原因,我不能这样做 test2 = "" function process(){ url = "http://www.example.com/example.html" var xhr = new XMLHttpRequest(); xhr.open("GET", url, true); xhr.onreadystatechange = function() { if (xh

我试图将xhr.responseText变成一个变量,然后提醒它,但由于一个奇怪的原因,我不能这样做

test2 = ""
function process(){
    url = "http://www.example.com/example.html"
    var xhr = new XMLHttpRequest();
    xhr.open("GET", url, true);
    xhr.onreadystatechange = function() {
    if (xhr.readyState == 4){
        test2 = xhr.responseText
        }
    }
    xhr.send();
}
process();
alert(test2);

感谢在这个问题上提供帮助的所有人。

您在test2变量充满内容之前发出警报(因为XMLHTTPRequest需要一些额外的时间才能完成)


您正在触发一个异步请求。执行流程功能后,
警报将立即触发。稍后当响应返回时,
onreadystatechange
被激发。如果您计划对AJAX查询的结果执行任何操作,那么应该在
onreadystatechange
中执行。像这样

 test2 = ""
function process(){
    url = "http://www.example.com/example.html"
    var xhr = new XMLHttpRequest();
    xhr.open("GET", url, true);
    xhr.onreadystatechange = function() {
    if (xhr.readyState == 4){
        test2 = xhr.responseText
        alert(test2);
        }
    }
    xhr.send();
}
process();

同步调用通常不是一种好的做法。 也许jQuery是您可以使用的东西

例如:

$.ajax({
  url: "http://www.example.com/example.html",
  success: function(data) {
    // now you can do something with the data
    alert(data);
  }
});

alert(test2)
可以在
onreadystatechange
之前运行,因为后者是异步的,请在处理程序中执行您的工作或使用同步请求..嘿,Alex K.您能给我一个示例,说明如何将xhr.responseText设置为全局变量吗?非常感谢。谢谢。所以绝对没有办法使结果成为一个全局变量?我需要为脚本的另一部分存储它。您可以,但是您应该在
onreadystatechange
函数中执行“另一部分”,因为此时您将有responseText。@Tom结果仍存储在全局变量中,只有当你收到回复后,它才可用。如果我将true更改为false,会发生什么?它将首先完成,然后再进入代码的另一部分?@Tom-是的,你在那里找到了。这是一个“异步”标志。将其设置为false将使操作同步,这意味着除非此操作完成,否则不会运行其他代码,如果浏览器是一个长时间运行的进程,则会有效地锁定浏览器。小心使用。我需要将其存储为全局变量,因为我需要在脚本的后面部分使用它。这正是代码所做的。。。由于xhr.onreadystatechange是异步的,因此在调用该xhr.onreadystatechange之前,您无法访问内容
$.ajax({
  url: "http://www.example.com/example.html",
  success: function(data) {
    // now you can do something with the data
    alert(data);
  }
});