响应 接口在 抓取 API represents the response to a request.

You can create a new 响应 对象使用 Response.Response() constructor, but you are more likely to encounter a 响应 object being returned as the result of another API operation—for example, a service worker Fetchevent.respondWith , or a simple WindowOrWorkerGlobalScope.fetch() .

构造函数

Response()
创建新的 响应 对象。

特性

Response.headers 只读
object associated with the response.
Response.ok 只读
A boolean indicating whether the response was successful (status in the range 200 299 ) or not.
Response.redirected 只读

Indicates whether or not the response is the result of a redirect (that is, its URL list has more than one entry).

Response.status 只读
The status code of the response. (This will be 200 for a success).
Response.statusText 只读
The status message corresponding to the status code. (e.g., OK for 200 ).
Response.trailers
Promise resolving to a object, associated with the response with Response.headers for values of the HTTP Trailer 头。
Response.type 只读
The type of the response (e.g., basic , cors ).
Response.url 只读

The URL of the response.

Response.useFinalURL

A boolean indicating whether this is the final URL of the response.

Body Interface Properties

响应 实现 Body , so it also has the following properties available to it:

Body.body 只读
A simple getter exposing a ReadableStream of the body contents.
Body.bodyUsed 只读
存储 布尔 that declares whether the body has been used in a response yet.

方法

Response.clone()
Creates a clone of a 响应 对象。
Response.error()
返回新的 响应 object associated with a network error.
Response.redirect()

Creates a new response with a different URL.

Body Interface Methods

响应 实现 Body , so it also has the following methods available to it:

Body.arrayBuffer()
Takes a 响应 stream and reads it to completion. It returns a promise that resolves with an ArrayBuffer .
Body.blob()
Takes a 响应 stream and reads it to completion. It returns a promise that resolves with a Blob .
Body.formData()
Takes a 响应 stream and reads it to completion. It returns a promise that resolves with a FormData 对象。
Body.json()
Takes a 响应 stream and reads it to completion. It returns a promise that resolves with the result of parsing the body text as JSON .
Body.text()
Takes a 响应 stream and reads it to completion. It returns a promise that resolves with a USVString (text).

范例

In our basic fetch example ( run example live ) we use a simple fetch() call to grab an image and display it in an <img> element. The fetch() call returns a promise, which resolves to the 响应 object associated with the resource fetch operation.

You'll notice that since we are requesting an image, we need to run Body.blob ( 响应 implements Body) to give the response its correct MIME type.

const image = document.querySelector('.my-image');
fetch('flowers.jpg').then(function(response) {
  return response.blob();
}).then(function(blob) {
  const objectURL = URL.createObjectURL(blob);
  image.src = objectURL;
});
						

还可以使用 Response.Response() constructor to create your own custom 响应 对象:

const response = new Response();
						

规范

规范 状态 注释
Fetch
The definition of 'Response' in that specification.
实时标准 初始定义

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
响应 Chrome 42
42
41 Disabled
Disabled From version 41: this feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.
Edge ≤18 Firefox 39
39
34 Disabled
Disabled From version 34: this feature is behind the dom.fetch.enabled preference. To change preferences in Firefox, visit about:config.
IE No Opera 29
29
28 Disabled
Disabled From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari 10.1 WebView Android 42 Chrome Android 42
42
41 Disabled
Disabled From version 41: this feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.
Firefox Android 39
39
34 Disabled
Disabled From version 34: this feature is behind the dom.fetch.enabled preference. To change preferences in Firefox, visit about:config.
Opera Android 29
29
28 Disabled
Disabled From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari iOS 10.3 Samsung Internet Android 4.0
Response() 构造函数 Chrome 42
42
41 Disabled
Disabled From version 41: this feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.
Edge 15 Firefox 39
39
34 Disabled
Disabled From version 34: this feature is behind the dom.fetch.enabled preference. To change preferences in Firefox, visit about:config.
IE No Opera 29
29
28 Disabled
Disabled From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari 10.1 WebView Android 42 Chrome Android 42
42
41 Disabled
Disabled From version 41: this feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.
Firefox Android Yes Opera Android 29
29
28 Disabled
Disabled From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari iOS 10.3 Samsung Internet Android 4.0
clone Chrome 42
42
41 Disabled
Disabled From version 41: this feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.
Edge 14 Firefox 39
39
34 Disabled
Disabled From version 34: this feature is behind the dom.fetch.enabled preference. To change preferences in Firefox, visit about:config.
IE No Opera 29
29
28 Disabled
Disabled From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari No WebView Android No Chrome Android No Firefox Android No Opera Android 29
29
28 Disabled
Disabled From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari iOS No Samsung Internet Android No
error Chrome Yes Edge 16 Firefox Yes IE No Opera Yes Safari Yes WebView Android No Chrome Android No Firefox Android No Opera Android No Safari iOS No Samsung Internet Android No
headers Chrome 42
42
41 Disabled
Disabled From version 41: this feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.
Edge 14 Firefox 39
39
34 Disabled
Disabled From version 34: this feature is behind the dom.fetch.enabled preference. To change preferences in Firefox, visit about:config.
IE No Opera 29
29
28 Disabled
Disabled From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari No WebView Android No Chrome Android 42 Firefox Android No Opera Android 29
29
28 Disabled
Disabled From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari iOS No Samsung Internet Android No
ok Chrome 42
42
41 Disabled
Disabled From version 41: this feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.
Edge 14 Firefox 39
39
34 Disabled
Disabled From version 34: this feature is behind the dom.fetch.enabled preference. To change preferences in Firefox, visit about:config.
IE No Opera 29
29
28 Disabled
Disabled From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari 10.1 WebView Android No Chrome Android No Firefox Android No Opera Android 29
29
28 Disabled
Disabled From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari iOS 10.3 Samsung Internet Android No
redirect Chrome Yes Edge 16 Firefox Yes IE No Opera Yes Safari Yes WebView Android No Chrome Android No Firefox Android No Opera Android No Safari iOS No Samsung Internet Android No
redirected Chrome 60 Edge 16 Firefox 49 IE No Opera 47 Safari No WebView Android 60 Chrome Android 60 Firefox Android 49 Opera Android 44 Safari iOS No Samsung Internet Android 8.0
status Chrome 42
42
41 Disabled
Disabled From version 41: this feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.
Edge 14 Firefox 39
39
34 Disabled
Disabled From version 34: this feature is behind the dom.fetch.enabled preference. To change preferences in Firefox, visit about:config.
IE No Opera 29
29
28 Disabled
Disabled From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari 10.1 WebView Android No Chrome Android No Firefox Android No Opera Android 29
29
28 Disabled
Disabled From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari iOS 10.3 Samsung Internet Android No
statusText Chrome 42
42
41 Disabled
Disabled From version 41: this feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.
Edge 14 Firefox 39
39
34 Disabled
Disabled From version 34: this feature is behind the dom.fetch.enabled preference. To change preferences in Firefox, visit about:config.
IE No Opera 29
29
28 Disabled
Disabled From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari No WebView Android No Chrome Android No Firefox Android No Opera Android 29
29
28 Disabled
Disabled From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari iOS No Samsung Internet Android No
trailer Chrome No Edge No Firefox ? IE No Opera ? Safari ? WebView Android No Chrome Android No Firefox Android ? Opera Android ? Safari iOS ? Samsung Internet Android No
type Chrome 42
42
41 Disabled
Disabled From version 41: this feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.
Edge 14 Firefox 39
39
34 Disabled
Disabled From version 34: this feature is behind the dom.fetch.enabled preference. To change preferences in Firefox, visit about:config.
IE No Opera 29
29
28 Disabled
Disabled From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari No WebView Android No Chrome Android No Firefox Android No Opera Android 29
29
28 Disabled
Disabled From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari iOS No Samsung Internet Android No
url Chrome 42
42
41 Disabled
Disabled From version 41: this feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.
Edge 14 Firefox 39
39
34 Disabled
Disabled From version 34: this feature is behind the dom.fetch.enabled preference. To change preferences in Firefox, visit about:config.
IE No Opera 29
29
28 Disabled
Disabled From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari No WebView Android No Chrome Android No Firefox Android No Opera Android 29
29
28 Disabled
Disabled From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari iOS No Samsung Internet Android No
useFinalURL 弃用 Chrome 42
42
41 Disabled
Disabled From version 41: this feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.
Edge ≤18 Firefox 39
39
34 Disabled
Disabled From version 34: this feature is behind the dom.fetch.enabled preference. To change preferences in Firefox, visit about:config.
IE No Opera 29
29
28 Disabled
Disabled From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari No WebView Android No Chrome Android No Firefox Android 39
39
34 Disabled
Disabled From version 34: this feature is behind the dom.fetch.enabled preference. To change preferences in Firefox, visit about:config.
Opera Android 29
29
28 Disabled
Disabled From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari iOS No Samsung Internet Android No

图例

完整支持

完整支持

不支持

不支持

兼容性未知 ?

兼容性未知

实验。期望将来行为有所改变。

实验。期望将来行为有所改变。

弃用。不要用于新网站。

弃用。不要用于新网站。

用户必须明确启用此特征。

用户必须明确启用此特征。

另请参阅

元数据

  • 最后修改:
  1. 抓取 API
  2. 响应
  3. 构造函数
    1. Response()
  4. 特性
    1. body
    2. bodyUsed
    3. headers
    4. ok
    5. redirected
    6. status
    7. statusText
    8. type
    9. url
    10. useFinalURL
  5. 方法
    1. arrayBuffer()
    2. blob()
    3. clone()
    4. error()
    5. formData()
    6. json()
    7. redirect()
    8. text()
  6. Related pages for Fetch API
    1. Body
    2. Request
    3. WindowOrWorkerGlobalScope.fetch()

版权所有  © 2014-2026 乐数软件    

工业和信息化部: 粤ICP备14079481号-1