Response.type

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 接口的 type 只读属性包含一种响应的类型。它可以是以下某一种值:

  • basic:标准值,同源响应,暴露除了“Set-Cookie”之外的所有标头。
  • cors:从有效的跨源请求接收到响应。某些标头和主体可以被访问。
  • error:网络错误。没有有用的描述错误的信息。响应的状态为 0,header 为空且不可变。这是从 Response.error() 中获得的响应的类型。
  • opaque:对跨源资源的“no-cors”请求的响应。严格限制
  • opaqueredirect:fetch 请求是通过 redirect: "manual" 发出的。响应的状态是 0,标头是空的,主体是 null,trailer 是空的。

备注: “错误”响应从来没有真正暴露于脚本:这样的响应,fetch() 将被 promise 拒绝。

ResponseType 字符串表示响应的类型。

示例

在我们的 Fetch 响应示例中(参见 Fetch 响应在线演示),我们使用 Request() 构造函数创建了一个新的 Request 对象,并向其传递了一个 JPG 路径。然后,我们使用 fetch() 获取到这个请求,使用 Response.blob 从响应中提取到 blob,使用 URL.createObjectURL 从中创建一个对象的 URL,并将其显示在 <img> 中。

注意,在 fetch() 代码块的顶部,我们将响应的 type 记录在控制台。

js
const myImage = document.querySelector("img");
const myRequest = new Request("flowers.jpg");
fetch(myRequest).then((response) => {
  console.log(response.type); // returns basic by default
  response.blob().then((myBlob) => {
    const objectURL = URL.createObjectURL(myBlob);
    myImage.src = objectURL;
  });
});

规范

Specification
Fetch
# ref-for-dom-response-type①

浏览器兼容性

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
type

Legend

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

Full support
Full support

参见