Google Chrome无法在Connection关闭时加载XMLHttpRequest响应

时间:2018-01-12 13:58:15

标签: javascript ajax google-chrome http xmlhttprequest

我正在使用XMLHttpRequest(AJAX)实现“加载更多”功能。 Web服务器将使用Keep-Alive响应这些调用:timeout = 5,max = 10和Connection:keep-alive标头。在10响应上,它按预期发送Connection:close标头。除Chrome之外,所有浏览器(Safari,Opera,Firefox)都会继续处理响应。它以状态代码0停止,没有响应文本。

以下是JavaScript代码段:

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) {}
}

为什么Chrome会以这种方式表现出来?

1 个答案:

答案 0 :(得分:0)

原来在Chrome中,超时是渐进式的,这意味着每次请求都不会重置。