怎樣監聽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