XmlHttpRequest 过早返回状态 4

发布于 2024-10-31 15:00:13 字数 1705 浏览 1 评论 0原文

我正在开发一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏末染殇 2024-11-07 15:00:14

3 个建议:

  1. 为了避免客户端进行任何缓存,请在请求查询字符串中添加随机生成的数字或当前时间戳。
  2. 正如 Yoni 所说,使用 var 关键字启动您的 XMLHttpRequest 对象。
  3. 对于每个请求,将当前时间戳保存在全局变量中。在 onreadystatechange 中,仅当全局时间戳与给定请求的相应时间戳匹配时才调用 activateScanButtons。这样,只有最新的请求才能调用 activateScanButtons

3 suggestions:

  1. To avoid any caching on the client side, add a randomly generated number, or the current timestamp to the request querystring.
  2. As Yoni said, initiate your XMLHttpRequest object with var keyword.
  3. For each request, save the current timestamp within a global variable. In onreadystatechange, call activateScanButtons only if the global timestamp matches the corresponding timestamp of that given request. This way, only the latest request will be able to call activateScanButtons.
夏九 2024-11-07 15:00:14

您可以在 scan 函数中定义 ajax 对象,而无需在其前面添加 var 关键字。这意味着它是一个全局对象,而不是局部对象。之后,您将获得一个闭包 - 您可以在 onreadystate 回调函数中引用该变量。

我发现很难准确跟踪那里发生的事情,但我同意你的观点,正如你在问题中所说,回调没有按照你期望的方式使用 ajax 对象。你说它有时会发生 - 当你几乎同时发出两个请求时(双击按钮或以其他方式非常快地触发两个获取请求),它会发生吗?

我建议您在定义 ajax 对象之前使用 var 关键字。更好的是,尝试在回调函数中使用 this 而不是通过名称引用 ajax 对象。如果它有效,那么你就避免了一次关闭。

You define the ajax object in scan function without the var keyword before it. This means that it is a global object, not local. Afterwards, you have a closure - you refer to that variable in the onreadystate 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 using this in the callback function instead of referring to the ajax object by name. If it works, you have spared yourself of one closure.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文