回答
Fetch可以將包含許多自定義設置的init對象包含在您可能想要應用於該請求中,其中包括一個名爲「headers」的選項。
「標題」選項需要一個Header對象。該對象允許您配置要添加到請求中的標頭。
通過添加編譯:無緩存和緩存控制:無緩存到你的頭,你將迫使瀏覽器來檢查服務器是否該文件是從它已經在文件的不同緩存。您還可以使用緩存控制:no-store,因爲它只是不允許瀏覽器和所有中間緩存存儲任何返回的響應版本。
這裏是一個示例代碼:
var myImage = document.querySelector('img');
var myHeaders = new Headers();
myHeaders.append('pragma', 'no-cache');
myHeaders.append('cache-control', 'no-cache');
var myInit = {
method: 'GET',
headers: myHeaders,
};
var myRequest = new Request('myImage.jpg');
fetch(myRequest, myInit)
.then(function(response) {
return response.blob();
})
.then(function(response) {
var objectURL = URL.createObjectURL(response);
myImage.src = objectURL;
});
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>ES6</title>
</head>
<body>
<img src="">
</body>
</html>
希望這有助於。
優秀!非常感謝 – 2015-03-25 03:34:57
如何使用'new Request'並將一些參數傳遞給'cache'選項?我試圖使用它,但它不起作用。 – Mitar 2016-01-21 07:05:44
更容易使用的緩存模式:
// Download a resource with cache busting, to bypass the cache
// completely.
fetch("some.json", {cache: "no-store"})
.then(function(response) { /* consume the response */ });
// Download a resource with cache busting, but update the HTTP
// cache with the downloaded resource.
fetch("some.json", {cache: "reload"})
.then(function(response) { /* consume the response */ });
// Download a resource with cache busting when dealing with a
// properly configured server that will send the correct ETag
// and Date headers and properly handle If-Modified-Since and
// If-None-Match request headers, therefore we can rely on the
// validation to guarantee a fresh response.
fetch("some.json", {cache: "no-cache"})
.then(function(response) { /* consume the response */ });
// Download a resource with economics in mind! Prefer a cached
// albeit stale response to conserve as much bandwidth as possible.
fetch("some.json", {cache: "force-cache"})
.then(function(response) { /* consume the response */ });
refrence:https://hacks.mozilla.org/2016/03/referrer-and-cache-control-apis-for-fetch/
這是更合適的答案。您可以通過這些選項處理標題,如'If-Modified-Since'和'If-None-Match'。 – Nigiri 2017-03-15 07:57:55
這似乎在Firefox(54),但不是Chrome(60)。燃燒熔爐的答案確實奏效。 – Scimonster 2017-08-15 12:10:59
您可以設置'Cache-Control': 'no-cache'
在這樣的:頭
return fetch(url, {
headers: {
'Cache-Control': 'no-cache'
}
}).then(function (res) {
return res.json();
}).catch(function(error) {
console.warn('Failed: ', error);
});
- 1. 緩存非CORS響應衝突請求
- 2. 如何使用PHP製作api請求緩存?
- 3. 如何在PHP的JSON請求中製作緩存文件
- 4. 如何通過使用Http緩存控制保存firestore請求?
- 5. memcached緩存請求?
- 6. 緩存Tastypie請求
- 7. Hammock緩存請求?
- 8. 爲非IIS託管的WCF服務請求作用域緩存
- 9. 強制HTTP請求不緩存
- 10. .htacces高速緩存控制請求
- 11. 如何緩存外部json請求?
- 12. 如何緩存jquery ajax加載請求
- 13. 如何緩存SOAP請求,並在Java
- 14. 如何使用yesod每請求緩存?
- 15. jQuery - 如何緩存$ .get請求
- 16. 如何緩存與清漆的請求?
- 17. 如何製作CORS請求
- 18. 如何製作JSON請求?
- 19. 設置緩存控制:GET緩存請求
- 20. 如何製作NSURL緩存?
- 21. 如何緩存控制效果請求頭,在響應頭
- 22. 強制瀏覽器緩存對Castle MonoRail操作的請求?
- 23. ASP.NET MVC緩存如何爲AJAX請求工作?
- 24. IE緩存ajax請求
- 25. 緩存匿名HTTP請求
- 26. 無法緩存main_frame請求
- 27. 緩存休息請求
- 28. AFNetworking不緩存HTTPS請求
- 29. 緩存jquery獲取請求
- 30. jQuery的ajax請求緩存
你有一個參考*獲取*在[* ECMA-262 ed 6 draft *](https://people.mozilla.org/~jorendorff/es6-draft.html)?我沒看到它。或者你的意思是[* WHATWG Fetch生活標準*](https://fetch.spec.whatwg.org)? – RobG 2015-03-25 02:46:58
RobG - 使用https://developer.mozilla.org/en-US/docs/Web/API/FetchEvent – 2015-03-25 03:29:30