怎样监听HTTP请求的成功、失败与进行时

1. 监听请求成功: xhr.onload

2. 监听请求失败: xhr.onerror

3. 监听请求数据下载中: xhr.onprogress

xhr.onload = function() {
 var responseText = xhr.responseText;
 console.log(responseText);
 // process the response.
};

xhr.onabort = function () {
  console.log('The request was aborted');
};

xhr.onprogress = function (event) {
  console.log(event.loaded);
  console.log(event.total);
};

xhr.onerror = function() {
  console.log('There was an error!');
};

注意: xhr.onprogress是其中仅有的一个具有事件参数的事件监听属性, e.loaded表示下载了多少数据, e.total表示数据总量, e.lengthComputable 表示加载进度是否可计算.

原文地址:https://www.cnblogs.com/aisowe/p/11556867.html