Search code examples
javascriptajaxgoogle-chromehttpxmlhttprequest

Google Chrome fails to load XMLHttpRequest response on Connection close


I'm implementing a "load more" functionality, using XMLHttpRequest (AJAX). The webserver will respond to those calls with a Keep-Alive: timeout=5,max=10 and a Connection: keep-alive headers. On the 10 response it sends a Connection: close header, as expected. All browsers (Safari, Opera, Firefox) continue to process the response, except Chrome. It stops with a status code 0 and no response text.

Here is the JavaScript snippet:

function send(method, data) {
    try {
        let this._request = null;
        let created = false;
        if (!(this._request instanceof XMLHttpRequest)) {
            this._request = new XMLHttpRequest();
            this._request.onreadystatechange = (event) => {
                switch (this._request.readyState) {
                    case XMLHttpRequest.OPENED: {
                        // Do something
                    } break;
                    case XMLHttpRequest.LOADING: {
                        // Do something
                    } break;
                    case XMLHttpRequest.DONE: {
                        // Do something
                    } break;
                }
            };
            this._request.addEventListener(
                'error',
                (event) => {
                        // Do something
                }
            );
            this._request.addEventListener(
                'abort',
                (event) => {
                        // Do something
                }
            );
            created = true;
        }
        this._request.open(String(method), this.path);
        this._request.timeout = 200;
        if (created) {
            // Initialization is done here, like setting request headers...
        }
        this._request.send(data);
    } catch (error) {}
}

Any ideas why Chrome is behaving that way?


Solution

  • Turns out that in Chrome the timeout is progressive, meaning it's not reset for each request.