我一直尝试使用 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();
我们只在 xhttp.readyState 等于 1 的 open() 语句中进入 onreadystatechange 函数一次,但不在 send( ) 步骤。我应该认为它至少会抛出某种错误而不是什么也不做。
另外,作为一个实验,我故意给 open() 提供了一个错误的 url - 但同样没有回复。
谁能告诉我我可能做错了什么?
非常感谢。
最佳答案
您的代码对我来说看起来是正确的,这表明存在一些外部原因。
您的代码是否一直流到执行上下文的末尾?浏览器将保留网络请求,直到引擎让渡给浏览器。
例如:
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){}
永远不会调用电话。
关于javascript - XMLHttpRequest 似乎没有做任何事情,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34620716/