Response()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since March 2017.

Response() 构造函数创建了一个新的 Response 对象。

语法

let myResponse = new Response(body, init);

参数

body 可选

一个定义 response 中 body 的对象。可以为 null ,或是以下其中一个:

init 可选

一个参数 (options) 对象,包含要应用到 response 上的任何自定义设置。可能参数 (options) 是:

  • status: response 的状态码,例如:200.
  • statusText: 和状态码关联的状态消息,例如:OK.
  • headers: 你想加到 response 上的任何 headers,包含了一个 Headers 对象或满足对象语法的 ByteString key/value 对 (详见 HTTP headers).

例子

在我们的 Fetch Response 示例中 (参见Fetch Response live) 我们使用构造函数创建了一个新的 Response 对象,传递一个新的 Blob 作为 body,和一个包含自定义 statusstatusText的 init 对象:

js
var myBlob = new Blob();
var init = { status: 200, statusText: "SuperSmashingGreat!" };
var myResponse = new Response(myBlob, init);

规范

Specification
Fetch
# ref-for-dom-response①

浏览器兼容性

Report problems with this compatibility data on GitHub
desktopmobileserver
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
Node.js
Response() constructor
body parameter accepts ReadableByteStream
body parameter is optional

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

See also