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

评论关闭
IT源码网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!