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 表示加載進度是否可計算.