W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗(yàn)值獎勵
Request
Fetch API的Request接口代表一個資源請求。
您可以使用Request.Request()構(gòu)造函數(shù)創(chuàng)建一個新的Request對象,但是您更可能遇到由于另一個API操作(如service worker,FetchEvent.request
)而返回的Request對象。
Request.Request()
Request
對象。Request.method
只讀GET
,POST
等)Request.url
只讀Request.headers
只讀Request.context
只讀 audio
,image
,iframe
,等等)Request.referrer
只讀client
)。Request.referrerPolicy
只讀no-referrer
)。Request.mode
只讀cors
,no-cors
,same-origin
,navigate
。)Request.credentials
只讀omit
,same-origin
)。Request.redirect
只讀follow
,error
或manual
。Request.integrity
只讀sha256-BpfBw7ivV8q2jLiT13fxDYAe2tJllusRSZ273h2nFSE=
)。Request.cache
只讀default
,reload
,no-cache
)。Request實(shí)現(xiàn)Body,所以它也有以下屬性可用:
Body.body
只讀ReadableStream
正文內(nèi)容。Body.bodyUsed
只讀Boolean
聲明是否已經(jīng)在響應(yīng)中使用了該機(jī)構(gòu)。Request.clone()
Request
對象的副本。Request實(shí)現(xiàn)Body,所以它也有以下方法可用:
Body.arrayBuffer()
ArrayBuffer
表現(xiàn)解決。Body.blob()
Blob
表現(xiàn)解決。Body.formData()
FormData
表現(xiàn)解決。Body.json()
JSON
表現(xiàn)解決。Body.text()
USVString
(文本)表現(xiàn)解決。注意:這些Body功能只能運(yùn)行一次;隨后的調(diào)用將用空字符串/ ArrayBuffers解決。
在下面的代碼片段中,我們使用Request()構(gòu)造函數(shù)創(chuàng)建一個新的請求(對于與腳本相同的目錄中的圖像文件),然后返回請求的一些屬性值:
const myRequest = new Request('http://localhost/flowers.jpg');
const myURL = myRequest.url; // http://localhost/flowers.jpg
const myMethod = myRequest.method; // GET
const myCred = myRequest.credentials; // omit
然后,您可以通過將Request對象作為參數(shù)傳遞給GlobalFetch.fetch()調(diào)用來獲取此請求,例如:
fetch(myRequest)
.then(response => response.blob())
.then(blob => {
myImage.src = URL.createObjectURL(blob);
});
在下面的代碼片段中,我們使用Request()帶有一些初始數(shù)據(jù)和正文內(nèi)容的構(gòu)造函數(shù)創(chuàng)建了一個新請求,用于需要主體負(fù)載的api請求:
const myRequest = new Request('http://localhost/api', {method: 'POST', body: '{"foo":"bar"}'});
const myURL = myRequest.url; // http://localhost/api
const myMethod = myRequest.method; // POST
const myCred = myRequest.credentials; // omit
const bodyUsed = myRequest.bodyUsed; // true
注意:body類型只能是一個Blob,BufferSource,F(xiàn)ormData,URLSearchParams,USVString或 ReadableStream類型,因此增加一個JSON對象的有效載荷則需要字符串化該對象。
然后,您可以通過將Request對象作為參數(shù)傳遞給GlobalFetch.fetch()調(diào)用來獲取此api請求,例如獲取響應(yīng):
fetch(myRequest)
.then(response => {
if (response.status === 200) {
return response.json();
} else {
throw new Error('Something went wrong on api server!');
}
})
.then(response => {
console.debug(response);
// ...
}).catch(error => {
console.error(error);
});
規(guī)范 | 狀態(tài) | 注釋 |
---|---|---|
Fetch 該規(guī)范中“請求”的定義。 | Living Standard | Initial definition |
Feature | Chrome | Edge | Firefox(Gecko) | Internet Explorer | Opera | Safari(WebKit) |
---|---|---|---|---|---|---|
基本的支持 | 支持:42 | (是) | 支持:39、34 | 不支持 | 支持:28 | 不支持 |
Request.body.formData | 支持:60 | ? | ? | 不支持 | 支持:47 | 不支持 |
Request.integrity | 支持:46 | (是) | (是) | 不支持 | 支持:33 | 不支持 |
Request.redirect | 支持:46 | (是) | (是) | 不支持 | 支持:33 | 不支持 |
構(gòu)造函數(shù)init 可以接受:ReadableStream body | 支持:43 | (是) | 不支持[1] | 不支持 | 支持:33 | 不支持 |
Feature | Android Webview | Chrome for Android | Edge | Firefox Mobile (Gecko) | IE Phone | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|---|
基本的支持 | 支持:42 | 支持:42 | (是) | (是) | 不支持 | 支持:28 | 不支持 |
Request.body.formData | 支持:60 | 支持:60 | ? | ? | 不支持 | 支持:47 | 不支持 |
Request.integrity | 支持:46 | 支持:46 | ? | (是) | 不支持 | 支持:33 | 不支持 |
Request.redirect | 支持:46 | 支持:46 | ? | (是) | 不支持 | 支持:33 | 不支持 |
構(gòu)造函數(shù) init 可以接受:ReadableStream body | 支持:43 | 支持:43 | (是) | 不支持[1] | 不支持 | 支持:33 | 不支持 |
注解:
[1]可讀流目前在Firefox中啟用,但隱藏在dom.streams.enabled和javascript.options.streamsprefs 后面。
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: