MDN’s new design is in Beta! A sneak peek: https://blog.mozilla.org/opendesign/mdns-new-design-beta/

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

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

Fetch API 的 Request 接口用来表示资源请求。
 

你可以使用  Request.Request() 构造函数创建一个 Request 对象,但是你可能会遇到一个 Request 对象作为其它 API 的操作被返回,比如一个 service worker FetchEvent.request

构造器

Request.Request()
创建一个新的 Request 对象。

属性

Request.method 只读
请求使用的方法 (GET, POST, 等.)
Request.url 只读
请求使用的 URL。
Request.headers 只读
请求所关联的 Headers 对象。
Request.context 只读  
请求的上下文 例如:(例如:audio, image, iframe, 等)
Request.referrer 只读
请求的来源 (例如:client).
Request.mode 只读
请求的模式 (例如: cors, no-cors, same-origin).
Request.credentials 只读
请求的凭证 (例如: omit, same-origin).
Request.redirect 只读
如何处理重定向模式 (例如: follow, error, or manual)
Request.integrity 只读
请求内容的 subresource integrity 值 (例如: sha256-BpfBw7ivV8q2jLiT13fxDYAe2tJllusRSZ273h2nFSE=).
Request.cache 只读
请求的缓存模式 (例如: default, reload, no-cache).

Request implements Body, so it also has the following property available to it:

Body.bodyUsed 只读
指示body是否被使用, 类型为Boolean

方法

Request.clone()
创建当前request的副本。

Request implements Body, so it also has the following methods available to it:

Body.arrayBuffer()
Returns a promise that resolves with an ArrayBuffer representation of the request body.
Body.blob()
Returns a promise that resolves with an Blob representation of the request body.
Body.formData()
Returns a promise that resolves with an FormData representation of the request body.
Body.json()
Returns a promise that resolves with an JSON representation of the request body.
Body.text()
Returns a promise that resolves with an USVString (text) representation of the request body.

Note: The Body functions can be run only once; next calls will resolve with empty strings/ArrayBuffers.

示例

In the following snippet, we create a new request using the Request() constructor (for an image file in the same directory as the script), then return some property values of the request:

var myRequest = new Request('flowers.jpg');

var myURL = myRequest.url; // http://localhost:8000/flowers.jpg
var myMethod = myRequest.method; // GET
var myCred = myRequest.credentials; // omit

You could then fetch this request by passing the Request object in as a parameter to a GlobalFetch.fetch() call, for example:

fetch(myRequest).then(function(response) {
      return response.blob();
    }).then(function(response) {
      var objectURL = URL.createObjectURL(response);
      myImage.src = objectURL;
    });

规范

Specification Status Comment
Fetch
Request
Living Standard Initial definition

浏览器兼容性

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support

42.0

39 (39)
34[1]
未实现

29
28[1]

未实现
Request.integrity 45.0   未实现   未实现
Request.redirect 46.0        
Feature Android Android Webview Firefox Mobile (Gecko) Firefox OS (Gecko) IE Phone Opera Mobile Safari Mobile Chrome for Android
Basic support 未实现 未实现 未实现 未实现 未实现 未实现 未实现 42.0
Request.integrity 未实现 未实现           45.0
Request.redirect 未实现 未实现           46.0

相关链接

文档标签和贡献者

 此页面的贡献者: mo-n, gafish, looch5
 最后编辑者: mo-n,