Javascript 缓存api-添加XMLHttpRequest对象
我刚刚开始使用javascript的缓存API。我需要在所有GET请求中设置一个头,因此我将请求对象传递给“add”方法,如下所示Javascript 缓存api-添加XMLHttpRequest对象,javascript,local-storage,session-storage,cacheapi,Javascript,Local Storage,Session Storage,Cacheapi,我刚刚开始使用javascript的缓存API。我需要在所有GET请求中设置一个头,因此我将请求对象传递给“add”方法,如下所示 //Request object construction let xmlHttp = new XMLHttpRequest(); xmlHttp.onreadystatechange = function() { if (xmlHttp.readyState == 4 &&
//Request object construction
let xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
console.log(xmlHttp.responseText);
}
};
xmlHttp.open("GET", reqURL, true);
xmlHttp.setRequestHeader('MY-HEADER',myHeaderValue);
xmlHttp.setRequestHeader('Content-Type', 'application/json');
xmlHttp.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
//Cache operations
let cacheObj = await
caches.open(cacheName),
result = await cacheObj.match(reqURL, options);
if(emberUtils.isNone(result)) {
cacheObj.add(xmlHttp);
} else {
console.log("match found");
console.log(result);
}
但是,请求不是构造的请求对象,而是与XMLHttpRequest一起发送,直接附加到基本url,如。请帮助我发送构造的请求对象或在请求缓存api中设置头
提前感谢:)为什么不使用
xmlHttp.setRequestHeader(“缓存控制”、“最大过时”)用于缓存http响应的代码>?不需要特定的函数。我的要求是缓存来自服务器的响应。“Cache-Control”头只会指示服务器可以发送过时的响应,对吗@绝对初学者