我一直尝试使用jquery-ajax从我的服务器下载二进制文件非常失败,我终于放弃了。所以现在我正在尝试使用XMLHttpRequest。但是,我甚至无法得到一个简单的例子。
奇怪的是,这段代码似乎没有做任何事情。我从w3schools复制/粘贴此示例,此示例与许多其他示例几乎相同。它在chrome或FF中对我不起作用:
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
// Action to be performed when the document is read;
}
};
xhttp.open("GET", '/blah/blah/output.png', true);
xhttp.send();
我们只在 open()语句中进入onreadystatechange函数一次,其中 xhttp.readyState等于一个,但不在发送( )步骤。我认为它至少会引发某种错误而不是什么都不做。
另外,作为一个实验,我故意给open()一个坏网址 - 但是再没有回复。
是谁能告诉我自己可能做错了什么? 非常感谢你。答案 0 :(得分:2)
您的代码对我来说是正确的,这指出了一些外部原因。
您的代码是否一直流向执行上下文的末尾?浏览器将保留网络请求,直到引擎返回浏览器。
例如:
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
// Action to be performed when the document is read;
}
};
xhttp.open("GET", '/blah/blah/output.png', true);
xhttp.send();
while(true){}
永远不会发出电话。