views:

668

answers:

2

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);
    }
}
+1  A: 

I think you need to be creating a brand new XMLHttpRequest object for each request.

Pointy
I've seen it done both ways, but I wonder if it matters with POSTing. There's also the question of why it works on one web server (IIS6) and not another (IIS7). Nevertheless, it's a worthy suggestion. I'll give it a shot and let you know how it goes. Thanks!
Mike M. Lin
Thanks for the suggestion, Pointy, but the same problem occurs whether or not I use a new XHR object for each request.
Mike M. Lin
A: 
scunliffe
The baseline `Microsoft.XMLHTTP` is the same as `Msxml2.XMLHTTP.3.0` in most cases, though Microsoft would prefer you to use the namespace `Msxml2`. You only need to sniff for `6.0` if there's anything in that version you need (like XPath). Probably there isn't. See http://blogs.msdn.com/xmlteam/archive/2006/10/23/using-the-right-version-of-msxml-in-internet-explorer.aspx
bobince
It couldn't hurt to have the latest-and-greatest. I'll try them out.
Mike M. Lin
@bobince - thanks for the link, very informative
scunliffe
I changed my script to get the latest. As expected, it didn't help with my problem, which also occurs on Google Chrome which uses the proper XMLHttpRequest object.
Mike M. Lin