这是一个实验中的功能
此功能某些浏览器尚在开发中,请参考浏览器兼容性表格以得到在不同浏览器中适合使用的前缀。由于该功能对应的标准文档可能被重新修订,所以在未来版本的浏览器中该功能的语法和行为可能随之改变。
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, ormanual) 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
ArrayBufferrepresentation of the request body. Body.blob()- Returns a promise that resolves with an
Blobrepresentation of the request body. Body.formData()- Returns a promise that resolves with an
FormDatarepresentation of the request body. Body.json()- Returns a promise that resolves with an
JSONrepresentation 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.
示例
在下面的代码中,我们使用 Request ( ) 构造函数创建了一个新的 request 实例 (用来请求同一目录下的图片), 然后返回 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 |
未实现 |
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 |