怎样监听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 表示加载进度是否可计算.


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM