简体   繁体   中英

XMLHttpRequest POST stuck at readyState 1 after several successive requests against IIS7+Windows 7 Pro

I'm using JavaScript to send textarea updates back to a server in real-time. My HTML page uses a single XMLHttpRequest object to POST data to the server asynchronously. The messages are sent potentially at every onKeyUp event, but because I only use a single XMLHttpRequest object I throttle the requests by only POSTing when the readyState is either 0 (uninitialized) or 4 (loaded).

Against IIS6 on Windows 2003 Server, everything is working fine. Against IIS7 on Windows 7 Professional, the readyState of my XMLHttpRequest object gets stuck at 1 (initialized) if I type in several characters quickly.

The number of characters I can get in varies from 3 to 20 or so. If I reload the page, I can enter in a few more characters until I get stuck again. If I type slowly, then I can enter more characters before I see the problem. [Could this have anything to do with connection limits on IIS7?]

Also, if I use GET instead of POST, everything works fine (except that GET won't support the length of data I need to send).

Here's a snippet of the important bits of code here. Anyone have any insight into this problem? Thanks in advance.

var _xml = getXmlHttpRequest();

function getXmlHttpRequest()
{
    if (window.XMLHttpRequest)
        return new XMLHttpRequest();
    else if (window.ActiveXObject)
        return new ActiveXObject("Microsoft.XMLHTTP");
    alert ("Upgrade your browser, Fool!");
    return null;
}

function postValue(value)
{   // sends the answer to the server for saving
    if (_xml.readyState == 4 || _xml.readyState == "0")
    {
        var params = "value=" + encodeURIComponent(value);
        _xml.open("post", "save.py", true);
        _xml.onreadystatechange = handlePostValue;
        _xml.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        _xml.setRequestHeader("Content-Length", params.length);
        _xml.setRequestHeader("Connection", "close");
        _xml.send(params);
    }
}

我认为您需要为每个请求创建一个全新的XMLHttpRequest对象。

I could skip the parameter problem for a short time. I have currently 3 form parameters to send. I devide them by using a character that shows wich form stack element was hit, a numeric to show wich part of that stac was hit and a boolean for another reason.

Just a quick side note... would you not want better logic to support ActiveX xmlHTTP?

eg what about these versions (in descending order of priority)?

Msxml2.XMLHTTP.6.0
Msxml2.XMLHTTP.3.0
Msxml2.XMLHTTP
Microsoft.XMLHTTP

Update: One other option... I know that (at least in the past) some browsers had issues with the initialization of the readyState property... whereby it didn't have a value set.

Does changing this line help?

//was
if (_xml.readyState == 4 || _xml.readyState == "0")

//try this
if (typeof(_xml.readyState) == 'undefined' || 
    _xml.readyState == 4 || _xml.readyState == "0")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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