为什么XMLHttpRequest实例只能发送一个请求?

Why can XMLHttpRequest instance send only one request?

本文关键字:一个 请求 实例 XMLHttpRequest 为什么      更新时间:2023-09-26

我正试图从XMLHttpRequest对象的单个实例发送几个请求,如下所示:

GC3D.Application.prototype.setMapTiles = function() {
    var ajaxInstance = new GC3D.Ajax();
    for ( var i = 0, j = 0; i < 10; i++ ) {
        var urlFinal = this.prepareRequestForTileDownload( i, j );
        ajaxInstance.sendRequest({
            id: { i: i, j: j },
            HttpMethod: 'GET',
            UrlEndpoint: urlFinal
        }).then( function( item ) {
                   //...
                });
       }
};

它不工作,只发送一个请求。但是!如果要将源代码更改为:

for ( var i = 0, j = 0; i < 10; i++ ) {
    var ajaxInstance = new GC3D.Ajax();
...

它开始发送与for循环迭代总数一样多的请求,并且一切都工作得很好。我更喜欢了解一些功能。在c#开发在过去我从来没有一些TCP套接字的创建一个新的实例循环,如果我想让它异步,我创建了一个实例代表异步功能,如果有类似的情况在c#项目像JavaScript代码在问题的内容,因为它不会产生新对象在for循环,它需要更少的内存和体系结构表示为更清洁和良好的解决方案。

GC3D.Ajax定义为下一个原型:

GC3D.Ajax = function() {
    this.httpRequest = undefined;
    this.listExceptions = undefined;
    this.init();
};
GC3D.Ajax.prototype.init = function() {
    this.listExceptions = [];
    if ( window.XMLHttpRequest ) this.httpRequest = new XMLHttpRequest();
    else if ( window.ActiveXObject ) {
        try {
            this.httpRequest = new ActiveXObject( 'Microsoft.XMLHTTP' );
        }
        catch ( exception ) {
            this.listExceptions.push( exception );
            try {
                this.httpRequest = new ActiveXObject( 'Msxml2.XMLHTTP' );
            } 
            catch ( exception ) {
                this.listExceptions.push( exception );
                try {
                    this.httpRequest = new ActiveXObject( 'Microsoft.XMLHTTP' );
                } 
                catch ( exception ) {
                    this.listExceptions.push( exception );
                }
            }
        }
    }
    if ( !this.httpRequest ) {
        console.error( 'Can''t create a HTTP Request instance for AJAX! Possible problems:' );
        console.error( this.listExceptions );
    }
};
GC3D.Ajax.prototype.sendRequest = function( properties ) {
    var defer = new GC3D.Defer();
    if ( this.httpRequest !== undefined ) {
        this.httpRequest.onreadystatechange = function() {
            if ( this.httpRequest.readyState === 4 && this.httpRequest.status === 200 ) {
                var objectOutput = {};
                objectOutput.id = properties.id;
                objectOutput.content = this.httpRequest.responseText;
                defer.resolve( objectOutput );
            }
            else {
                var message = 'There was a problem with the request in GC3D.Ajax.';
                defer.reject( message );
            }
        }.bind( this );
        this.httpRequest.open( properties.HttpMethod, properties.UrlEndpoint );
        this.httpRequest.send();
    }
    else console.error( 'HTTP Request instance isn''t defined!' );
    return defer.promise;
};
GC3D.Ajax.prototype.get = function() {
    return this.httpRequest;
};

XMLHttpRequest API定义得很好,对这种行为没有任何疑问。

open()方法的定义,步骤13,声明该方法应该:

Terminate the request. 

http://www.w3.org/TR/XMLHttpRequest/open()方法

在浏览器中打开调试器并查看网络流量显示,实际上有几个请求正在异步发送,除了最后一个请求外,每个请求都被下一个请求取消。

在本例中,open()方法在ajaxInstancesendRequest方法中的XMLHttpRequest的相同实例上被重复调用。