gpt4 book ai didi

javascript - 在javascript中关闭缓存

转载 作者:行者123 更新时间:2023-11-30 08:46:02 25 4
gpt4 key购买 nike

大家好,我正在尝试关闭缓存将随机值添加到与请求消息一起发送的 URL 的查询字符串部分。

我有一个服务器将 etag 作为字符串发送到我的客户端,我想确保没有缓存在进行我已经设置了 RequestHeaders 但我还应该添加一个类似于 POST/message?x= 的 http 请求0.123456789 HTTP/1.1

这是我的客户端代码

<html>
<header><title>This is title</title></header>
<body>
<span id="ajaxButton" style="cursor: pointer; text-decoration: underline">
Make a request
</span>
<script type="text/javascript">
(function() {
var httpRequest;
var x= Math.random();
document.getElementById("ajaxButton").onclick = function() { makeRequest('http://localhost:5000/'); };
function makeRequest(url) {
if (window.XMLHttpRequest) { // Mozilla, Safari, ...
httpRequest = new XMLHttpRequest();
} else if (window.ActiveXObject) { // IE
try {
httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
try {
httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) {}
}
}
if (!httpRequest) {
alert('Giving up :( Cannot create an XMLHTTP instance');
return false;
}
httpRequest.onreadystatechange = alertContents;
httpRequest.open('GET', url, true);
//httpRequest.setRequestHeader("pragma", "no-cache");
//httpRequest.setRequestHeader("Cache-Control", "no-cache", "no-store");
httpRequest.send();

}
function alertContents() {
if (httpRequest.readyState === 4) {
if (httpRequest.status === 200) {
var etagString = httpRequest.responseText;
alert(etagString);
} else {
alert('There was a problem with the request.');
}
}
}
})();
</script>
</body>
</html>

编辑添加错误

XMLHttpRequest cannot load http://localhost:5000/?_0.1909303846769035. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. 

使用 node.js 我使用 main.js 运行服务器

var http = require('http');
var domain = require('domain');
var root = require('./root'); // do I have to replace root w/ message
var image = require('./image'); // for better readability?


function replyError(res) {
try {
res.writeHead(500);
res.end('Server error.');
} catch (err) {
console.error('Error sending response with code 500.');
}
};

function replyNotFound(res) {
res.writeHead(404);
res.end('not found');
}

function handleRequest(req, res) {
console.log('Handling request for ' + req.url);
if (req.url === '/') {
root.handle(req, res);
}
if (req.url === '/image.png'){
image.handle(req, res);
}
else {
replyNotFound(res);
}
}



var server = http.createServer();

server.on('request', function(req, res) {
var d = domain.create();
d.on('error', function(err) {
console.error(req.url, err.message);
replyError(res);
});
d.run(function() { handleRequest(req, res)});
});

function listen(){
server.listen(5000);
}

root.init(listen);

在 root.js 里面是

var http = require('http');
var response = require('./response');
var body;
var etag;



exports.handle = function(req, res) {
if (req.headers['if-none-match'] === etag) {
console.log('returning 304');
return response.replyNotModified(res);
}
res.writeHead(200, {'Content-Type': 'text/plain',
'Content-Length': body.length,
"Access-Control-Allow-Origin":"*",
"Access-Control-Allow-Headers":"X-Requested-With",
'ETag' : etag
});
res.end(body);
}


exports.init = function(cb) {
require('fs').readFile('app.html', function(err, data) {
if (err) throw err;
etag = response.generateETag(data); //
body = etag;
console.log("init");
cb();
});
}

/*function generateETag(buffer) {
var shasum = require('crypto').createHash('sha1');
shasum.update(buffer, 'binary');
return shasum.digest('hex');
console.log(shasum.digest('hex'));
}
var replyNotModified = function(res) {
res.writeHead(304);
res.end();
};*/

错误在

最佳答案

因此,您遇到的错误与跨源资源共享有关,与缓存或查询字符串无关。看起来您正在尝试从 file:// url 进行 AJAX 调用,您不能这样做。

如果您从 Node.js 应用提供有问题的页面,则该消息应该消失。

如果您不能这样做,请将该应用设置为发送 CORS header 。你可以read about CORS in detail at MDN , 但简短的版本是您需要发送一个如下所示的 header (其中 otherdomain.com 是托管网页的位置):

Access-Control-Allow-Origin: http://otherdomain.com

请注意,您仍然需要通过 HTTP 提供页面;据我所知,您根本无法从通过 file:// URL 加载的页面执行 AJAX。

关于javascript - 在javascript中关闭缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21976681/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com