XMLHttpRequest: responseText 屬性
注意:此功能在 Web Workers 中可用,但 Service Workers 除外。
只讀的 XMLHttpRequest 屬性 responseText 返回傳送請求後從伺服器接收到的文字。
值
一個字串,包含使用 XMLHttpRequest 接收到的文字資料,如果請求失敗或尚未收到任何內容,則為空字串 ""。
在處理非同步請求時,responseText 的值始終包含從伺服器接收到的當前內容,即使由於資料尚未完全接收而導致其不完整。
當 readyState 的值為 XMLHttpRequest.DONE (4) 且 status 的值為 200 ("OK") 時,您就知道已收到全部內容。
異常
InvalidStateErrorDOMException-
如果
XMLHttpRequest.responseType未設定為空字串或"text",則會丟擲此異常。由於responseText屬性僅對文字內容有效,任何其他值都表示錯誤條件。
示例
js
const xhr = new XMLHttpRequest();
xhr.open("GET", "/server", true);
// If specified, responseType must be empty string or "text"
xhr.responseType = "text";
xhr.onload = () => {
if (xhr.readyState === xhr.DONE) {
if (xhr.status === 200) {
console.log(xhr.response);
console.log(xhr.responseText);
}
}
};
xhr.send(null);
規範
| 規範 |
|---|
| XMLHttpRequest # the-responsetext-attribute |
瀏覽器相容性
載入中…