Response.type

这是一个实验中的功能
此功能某些浏览器尚在开发中,请参考浏览器兼容性表格以得到在不同浏览器中适合使用的前缀。由于该功能对应的标准文档可能被重新修订,所以在未来版本的浏览器中该功能的语法和行为可能随之改变。

type 是Response 接口包含的一种响应类型,是只读属性.它可以是以下某一种值:

  • basic: 标准值, 同源响应, 带有所有的头部信息除了“Set-Cookie” 和 “Set-Cookie2″.
  • cors: Response 接收到一个有效的跨域请求. Certain headers and the body may be accessed.
  • error: Network error. No useful information describing the error is available. The Response’s status is 0, headers are empty and immutable. This is the type for a Response obtained from Response.error().
  • opaque: Response for “no-cors” request to cross-origin resource. Severely restricted.

Note: An "error" Response never really gets exposed to script: such a response to a fetch() would reject the promise.

Syntax

var myType = response.type;

Value

A ResponseType string indicating the type of the response.

Example

In our Fetch Response example (see Fetch Response live) we create a new Request object using the Request() constructor, passing it a JPG path. We then fetch this request using fetch(), extract a blob from the response using Body.blob, create an object URL out of it using URL.createObjectURL, and display this in an <img>.

Note that at the top of the fetch() block we log the response type to the console.

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

Specifications

Specification Status Comment
Fetch
type
Living Standard Initial definition

Browser compatibility

Feature Chrome Edge Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support 42
41[1]
(Yes) 39 (39)
34[2]
未实现

29
28[1]

未实现
Feature Android Edge Firefox Mobile (Gecko) Firefox OS (Gecko) IE Phone Opera Mobile Safari Mobile Chrome for Android
Basic support 未实现 (Yes) 未实现 未实现 未实现 未实现 未实现 未实现

[1] The implementation of this feature is behind the "Experimental Web Platform Features" preference in chrome://flags.

[2] The implementation of this feature is behind the preference dom.fetch.enabled in about:config, defaulting to false.

See also

文档标签和贡献者