为什么XMLHttpRequest实例只能发送一个请求?
Why can XMLHttpRequest instance send only one request?
我正试图从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()
方法在ajaxInstance
的sendRequest
方法中的XMLHttpRequest的相同实例上被重复调用。
相关文章:
- Wicket在另一个请求之后触发请求
- 如何使请求等待上一个请求完成
- Nodejs一个请求阻塞另一个请求
- 如何在调用下一个请求之前完成第一个Ajax Get请求
- 如何将请求响应传递给另一个请求
- 主干-用一个请求替换整个集合
- Node.js在一个请求中提供多个文件
- 在angular cordova应用程序中发布一个请求中的多个文件
- $http请求在一个请求中返回所有记录而不分页
- 有没有一种方法可以用一个请求加载多个iframe
- 如果用户发送另一个请求,请使用 .abort() 取消现有的 ajax 请求
- 如何使用 angularjs 将$http请求返回的数据用于同一控制器中的另一个请求
- Ajax 触发两次替换 Rails 4 中的最后一个请求内容
- 在完成上一个请求之前中止新的 AJAX 请求
- 如何阻止 ajax 重复上一个请求
- Rails:在一个请求中渲染 html 和 js
- Javascript 一个请求多个 JS 文件
- JavaScript在一个请求中处理不同的内容类型
- MongoDB-组合多个命令来发送和返回一个请求
- Instagram API没有在一个请求中提取所有数据