简体   繁体   English

Javascript:xmlhttprequest随机停留在就绪状态1

[英]Javascript: xmlhttprequest randomly stuck at Ready State 1

I've been working on a Windows gadget (meaning the "browser" is Internet Explorer) that queries specified subnet addresses for information. 我一直在研究Windows小工具(意思是“浏览器”是Internet Explorer),它查询指定的子网地址以获取信息。 Now, it sometimes does this at a relatively quick pace (roughly every 5 seconds) and it works well enough. 现在,它有时会以相对较快的速度(大约每5秒钟)完成这项工作并且运行良好。 However, sometimes it will get stuck at ready state 1 and will just stay there forever. 但是,有时它会停留在就绪状态1并将永远停留在那里。 Whenever the gadget tries to redo the function for getting the xmlhttprequest and getting information from it it will stay at state 1. This is easily replicable when opening multiple instances of the gadget and then closing all but one of them. 每当小工具尝试重做函数以获取xmlhttprequest并从中获取信息时,它将保持在状态1.当打开小工具的多个实例然后关闭除其中一个之外的所有实例时,这很容易复制。 At that point, the one that's still open will almost always get stuck in this state. 那时,仍然打开的那个几乎总是卡在这种状态下。 I feel like it might have something to do with them all accessing the same website, or it may just have to do with xmlhttprequests being stopped mid-transmission and that preventing another from working. 我觉得它们可能与他们都访问同一个网站有关,或者它可能与xmlhttprequests在传输中途停止并阻止其他人工作有关。 Below is the relevant code. 以下是相关代码。

//Reference to this for the inner function
var me = this;
var request = new XMLHttpRequest();
request.onreadystatechange = onReadyStateChange;
var url = this.url;
//Make the URL random to prevent being cached
url += ("&a=" + ((new Date()).getTime()));
Trace(DEBUG_COMM, "Sase.updateStatus url: " + url);
request.open("GET", url, true);
request.send();   // fire off the request, calls httpRequestReadyStateChange as things continue
Trace(DEBUG_COMM, "Request sent" + request.readyState); 
function onReadyStateChange() {Trace(DEBUG_COMM, "Sase.httpRequestReadyStateChange: state=" + request.readyState);
    if (4 == request.readyState) {
        Trace(DEBUG_COMM, "Sase.httpRequestReadyStateChange: status=" + request.status);

        if (request.status == 200) {
            Trace(DEBUG_COMM, "retrieved html: " + request.responseText);
            var results = request.responseText;
            var resultsString = request.responseText.toString();
            Trace(DEBUG_COMM, "results String: " + resultsString);
            me.ParseStatusData(resultsString);
        }
        else {
            //me.commError(request.status);
        }

        request = null;
    }
}

For those who stumble across this and need a concrete code example, here you go. 对于那些偶然发现并且需要一个具体代码示例的人来说,你走了。

I had the same problem and the solution was to re-use the XMLHttpRequest object, to ensure that any previous request was cancelled before initiating a new one. 我有同样的问题,解决方案是重新使用XMLHttpRequest对象,以确保在启动新请求之前取消任何先前的请求。 This won't work if you want to have multiple AJAX requests flying around but in my case triggering a new request meant that the last one was no longer needed. 如果您希望多个AJAX请求飞来飞去,但是在我的情况下触发新请求意味着不再需要最后一个请求,这将无效。

All of the requests on my page went through the same XMLHttpRequest wrapper method which looked like this; 我页面上的所有请求都经过了相同的XMLHttpRequest包装方法,如下所示;

//Declare the XMLHttpRequest object to be re-used
var global_xHttpRequest = null;

function xmlHttpHandler(type, params, complete, postData) {

   //Prevents an issue where previous requests get stuck and new ones then fail
   if (global_xHttpRequest == null) {
       global_xHttpRequest = new XMLHttpRequest();
   } else {
       global_xHttpRequest.abort();
   }

   //Parse out current URL
   var baseURL = window.location.host;
   var svc = "https://" + baseURL + "/processAction?";

   var url = svc + params;

   global_xHttpRequest.open(type, url, true);

   //Add the callback
   global_xHttpRequest.onreadystatechange = complete;

   global_xHttpRequest.send(postData);
}

Which can be used like this: 哪个可以这样使用:

   xmlHttpHandler("GET", params, completeFnc);

Well it looks like I figured it out. 嗯,看起来我想通了。 I had a feeling it was an unresolved request, since it only happens when instances of it are closed (meaning that if one of them is closed while in communication with the server it stays in communication forever and no one else can access the server) and it appears that is the case. 我感觉到这是一个未解决的请求,因为它仅在实例关闭时才会发生(这意味着如果其中一个实例在与服务器通信时处于关闭状态,则它将永远保持通信状态,其他任何人都无法访问服务器)看来情况就是这样。 I made some modifications to the code in multiple areas and basically what it comes down to is when the gadget closes it makes sure to abort all of the requests. 我在多个方面对代码进行了一些修改,基本上它归结为当小工具关闭时它确保中止所有请求。 The requests are now instance variables (to allow for the aborting of them), but are still made new everytime they are needed. 现在,这些请求是实例变量(以允许中止这些请求),但每次需要它们时,它们仍然是新的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM