XMLHttpRequest:如何强制缓存? [英] XMLHttpRequest: How to force caching?

查看:110
本文介绍了XMLHttpRequest:如何强制缓存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于我以前使用过jQuery的AjAX方法,因此我对XMLHttpRequests较新.但是,我需要在Web工作者中工作,现在我必须使用经典的XMLHttpRequest来解决性能问题.

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.

我正在尝试从jquery重建cache-属性.如果应该禁用缓存,请添加以下内容:

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)?

推荐答案

您可以设置多种标题来鼓励缓存,但是它们(包括您错误使用的Cache-Control)是响应标头,必须由服务器发送,而不是请求标头.

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.

使用Cache-Control的此类示例:

One such example of using Cache-Control:

Cache-Control: max-age=3600

面向Web作者和网站管理员的缓存教程对其进行了更深入的介绍.

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

这篇关于XMLHttpRequest:如何强制缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆