XMLHttpRequest.response

这篇翻译不完整。请帮忙从英语翻译这篇文章

草案
本页尚未完工.

XMLHttpRequest.response 属性返回响应的正文。 它可以是 ArrayBuffer, Blob, Document, JavaScript对象 或 一个 DOMString 类型,具体取决于  XMLHttpRequest.responseType  属性的值。如果请求未完成或未成功,响应值为null。但是,如果responseType的值设置为“text”或空字符串,则响应可以包含部分文本响应,而请求仍处于加载状态。

Value of responseType Data type of response property
"" DOMString (this is the default value)
"arraybuffer" ArrayBuffer
"blob" Blob
"document" Document
"json" JavaScript object, parsed from a JSON string returned by the server
"text" DOMString
"moz-blob" Used by Firefox to allow retrieving partial Blob data from progress events. This lets your progress event handler start processing data while it's still being received.
"moz-chunked-text"

Similar to "text", but is streaming. This means that the value in response is only available during dispatch of the "progress" event and only contains the data received since the last "progress" event.

When response is accessed during a "progress" event it contains a string with the data. Otherwise it returns null.

This mode currently only works in Firefox.

"moz-chunked-arraybuffer"

Similar to "arraybuffer", but is streaming. This means that the value in response is only available during dispatch of the "progress" event and only contains the data received since the last "progress" event.

When response is accessed during a "progress" event it contains a string with the data. Otherwise it returns null.

This mode currently only works in Firefox.

"ms-stream" Indicates that the response is part of a streaming download. It is supported only for download requests. This mode is available only in Internet Explorer.

Note: Starting with Gecko 11.0 (Firefox 11.0 / Thunderbird 11.0 / SeaMonkey 2.8), as well as WebKit build 528, these browsers no longer let you use the responseType attribute when performing synchronous requests. Attempting to do so throws an NS_ERROR_DOM_INVALID_ACCESS_ERR exception. This change has been proposed to the W3C for standardization.

Example

var url = 'somePage.html'; //A local page

function load(url, callback) {
  var xhr = new XMLHttpRequest();

  xhr.onreadystatechange = function() {
    if (xhr.readyState === 4) {
      console.log(xhr.response); //Outputs a DOMString by default
    }
  }

  xhr.open('GET', url, true);
  xhr.send('');
}
 

Specifications

Specification Status Comment
XMLHttpRequest Living Standard WHATWG living standard

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support ? ? ? ? ?
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support ? ? ? ? ? ?

 

文档标签和贡献者

 此页面的贡献者: xgqfrms-GitHub
 最后编辑者: xgqfrms-GitHub,