您要监听的事件
readystatechange在XHR对象上(不在XHR.upload上)。
readyState是
4当上载完成发送 ,并
在服务器关闭连接 。
loadend/
load在上载完成时触发,无论服务器是否关闭连接。仅供参考,以下是您可以收听以及触发的事件:
var xhr = new XMLHttpRequest(); // ... // do stuff with xhr // ... xhr.upload.addEventListener('loadstart', function(e) { // When the request starts. }); xhr.upload.addEventListener('progress', function(e) { // While sending and loading data. }); xhr.upload.addEventListener('load', function(e) { // When the request has *successfully* completed. // Even if the server hasn't responded that it finished. }); xhr.upload.addEventListener('loadend', function(e) { // When the request has completed (either in success or failure). // Just like 'load', even if the server hasn't // responded that it finished processing the request. }); xhr.upload.addEventListener('error', function(e) { // When the request has failed. }); xhr.upload.addEventListener('abort', function(e) { // When the request has been aborted. // For instance, by invoking the abort() method. }); xhr.upload.addEventListener('timeout', function(e) { // When the author specified timeout has passed // before the request could complete. }); // notice that the event handler is on xhr and not xhr.upload xhr.addEventListener('readystatechange', function(e) { if( this.readyState === 4 ) { // the transfer has completed and the server closed the connection. } });
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)