XMLHttpRequest: How to force caching?

强颜欢笑 提交于 2019-12-30 10:05:33

问题


I'm newer to XMLHttpRequests since I've previously used jQuery's AjAX method. However I need to work in a web worker and now I have to use the classic XMLHttpRequest for performance issues.

I'm trying to rebuild the cache-property from jquery. If cache should be disabled I add this:

xhr.setRequestHeader("Cache-Control", "no-cache");

But what header should I set if I want to force caching (not prevent)?


回答1:


There are a variety of headers you can set to encourage caching, but they (including Cache-Control which you are using incorrectly) are response headers that must be sent by the server and not request headers.

One such example of using Cache-Control:

Cache-Control: max-age=3600

This Caching Tutorial for Web Authors and Webmasters covers them in more depth.




回答2:


You can specify max-stale without an argument, in Cache-Control header of your request. From RFC 7234:

The max-stale request directive indicates that the client is willing to accept a response that has exceeded its freshness lifetime. If max-stale is assigned a value, then the client is willing to accept a response that has exceeded its freshness lifetime by no more than the specified number of seconds. If no value is assigned to max-stale, then the client is willing to accept a stale response of any age.



来源:https://stackoverflow.com/questions/28154796/xmlhttprequest-how-to-force-caching

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!