XmlHttpRequest 过早返回状态 4
我正在开发一个 JavaScript 代码,以便使用 ANT Galio 浏览器在嵌入式设备上运行。
理想情况下,我希望代码能够向另一台服务器发出获取请求。发出该 get 请求后,页面将不允许用户提交另一个 get 请求,直到收到前一个 get 请求的响应。
由于某种原因,有时我几乎立即收到 4 的 readState。就好像它正在评估前一个 XmlHttpRequest 对象,而不是新的对象。我做错了什么?
<script type="text/javascript">
var fail= function (env, resp, stat) {
alert(resp);
};
var succ= function (env, resp) {
};
var canScan = true;
/* start scan */
function scan (name) {
if (canScan) {
//deactivate button
deactivateScanButtons();
//let server know
ajax = new XMLHttpRequest();
var scanUrl = 'http://19X.1XX.X.XX:8080/scan/' + name
ajax.open('GET', scanUrl, true);
ajax.onreadystatechange = function() {
if (ajax.readyState==4) {
//allow button to work again
activateScanButtons();
alert("ready state 4");
};
};
ajax.send();
//initiate scan
xrxScanInitiateScan(
'http://127.0.0.1',
"ftp.xst",
false,
succ,
fail);
}
}
function deactivateScanButtons () {
// canScan = false;
var indicator = document.getElementById('buttons');
indicator.style.visibility = "hidden";
}
function activateScanButtons () {
// canScan = true;
var indicator = document.getElementById('buttons');
indicator.style.visibility = "visible";
}
</script>
I'm developing a javascript code to run on an embedded device using the ANT Galio browser.
Ideally, I'd like the code to make a get request to another server. After that get request is made, the page would not allow the user to submit another get request, until a response had been received from the previous get request.
For some reason sometimes I am receiving a readyState of 4 almost instantly. It is as though it is evaluating the previous XmlHttpRequest object and not the new one. What am I doing wrong?
<script type="text/javascript">
var fail= function (env, resp, stat) {
alert(resp);
};
var succ= function (env, resp) {
};
var canScan = true;
/* start scan */
function scan (name) {
if (canScan) {
//deactivate button
deactivateScanButtons();
//let server know
ajax = new XMLHttpRequest();
var scanUrl = 'http://19X.1XX.X.XX:8080/scan/' + name
ajax.open('GET', scanUrl, true);
ajax.onreadystatechange = function() {
if (ajax.readyState==4) {
//allow button to work again
activateScanButtons();
alert("ready state 4");
};
};
ajax.send();
//initiate scan
xrxScanInitiateScan(
'http://127.0.0.1',
"ftp.xst",
false,
succ,
fail);
}
}
function deactivateScanButtons () {
// canScan = false;
var indicator = document.getElementById('buttons');
indicator.style.visibility = "hidden";
}
function activateScanButtons () {
// canScan = true;
var indicator = document.getElementById('buttons');
indicator.style.visibility = "visible";
}
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
3 个建议:
var
关键字启动您的XMLHttpRequest
对象。onreadystatechange
中,仅当全局时间戳与给定请求的相应时间戳匹配时才调用activateScanButtons
。这样,只有最新的请求才能调用activateScanButtons
。3 suggestions:
XMLHttpRequest
object withvar
keyword.onreadystatechange
, callactivateScanButtons
only if the global timestamp matches the corresponding timestamp of that given request. This way, only the latest request will be able to callactivateScanButtons
.您可以在
scan
函数中定义 ajax 对象,而无需在其前面添加var
关键字。这意味着它是一个全局对象,而不是局部对象。之后,您将获得一个闭包 - 您可以在 onreadystate 回调函数中引用该变量。我发现很难准确跟踪那里发生的事情,但我同意你的观点,正如你在问题中所说,回调没有按照你期望的方式使用 ajax 对象。你说它有时会发生 - 当你几乎同时发出两个请求时(双击按钮或以其他方式非常快地触发两个获取请求),它会发生吗?
我建议您在定义 ajax 对象之前使用
var
关键字。更好的是,尝试在回调函数中使用this
而不是通过名称引用 ajax 对象。如果它有效,那么你就避免了一次关闭。You define the ajax object in
scan
function without thevar
keyword before it. This means that it is a global object, not local. Afterwards, you have a closure - you refer to that variable in theonreadystate
callback function.I find it hard to track exactly what's going on there, but I agree with you, as you say in your question, that the callback is not using the ajax object the way you expect. You say it happens sometimes - does it happen when you make two requests almost simultaneously (double-click a button or otherwise trigger the two get requests very fast)?
I suggest that you use the
var
keyword before defining the ajax object. Even better, try usingthis
in the callback function instead of referring to the ajax object by name. If it works, you have spared yourself of one closure.