禁用 AJAX 缓存

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6216234/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 11:01:04  来源:igfitidea点击:

Disable AJAX Caching

ajax

提问by Nik

I am in a bit of a pickle right now. I am building a web page that will get data from a CGI backend. I have no control over the CGI backend, nor the server (so no mod_headers or mod_expires). Also, because of the parameters to the script, I cannot append a unique value (like '&089u0af0d98) to each request. The requests are AJAX using the XmlHttpRequest object. I have tried to set the 'If-Modified-Since' and 'Cache-Control' request headers unsuccessfully. Does anybody have any other ideas for how I can prevent the AJAX response from being cached by the browser?

我现在有点难受。我正在构建一个将从 CGI 后端获取数据的网页。我无法控制 CGI 后端,也无法控制服务器(因此没有 mod_headers 或 mod_expires)。此外,由于脚本的参数,我无法向每个请求附加唯一值(如 '&089u0af0d98)。请求是使用 XmlHttpRequest 对象的 AJAX。我试图设置“If-Modified-Since”和“Cache-Control”请求标头失败。有没有人对如何防止 AJAX 响应被浏览器缓存有任何其他想法?

回答by Marc

You can send random parameters using POST, while sending the important vars using GET if you need to.

您可以使用 POST 发送随机参数,同时根据需要使用 GET 发送重要变量。

If you have problems with IE, I know that sending something with POST makes it to stop caching server responses

如果您在使用 IE 时遇到问题,我知道使用 POST 发送某些内容会使其停止缓存服务器响应

回答by Vikas

I use this javascript function ( which in turn uses jquery.ajax function ) the cache: false would do the trick. This works perfectly for me , may be you can give it a try

我使用这个 javascript 函数(它反过来使用 jquery.ajax 函数) cache: false 可以解决问题。这对我来说非常有效,也许你可以试一试

    function ajax_call(urlString)
    {
        ret_val="";
        $.ajax
        (
            {
                type: "GET",
                url: urlString,
                async:false,
                cache:false,
                dataType: 'html',
                success: function(msg)
                {
                    ret_val=msg;
                },
                error:function (xhr, textStatus, thrownError)
                {
                    ret_val=xhr.readyState;
                    alert("status=" +xhr.status);
                }
            }
        );
        return ret_val;
    } 

回答by Makassi

I used $.ajaxSetup({ cache: false });somewhere in my base html-page (default.aspx) in a non-frequent web-system and it worked fine. No pain-in-the-neck caching problems anymore.

$.ajaxSetup({ cache: false });在我的基本 html 页面 (default.aspx) 中的某个地方使用了一个不常用的网络系统,它运行良好。不再有令人头疼的缓存问题。

回答by Bart B

I ran into this today, and found that if you want to keep to using get, you can add a hidden form element to the page and have JS set it's value to the current timestamp before submitting the query to ajax.

我今天遇到了这个,发现如果你想继续使用 get,你可以在页面中添加一个隐藏的表单元素,并在将查询提交给 ajax 之前让 JS 将其值设置为当前时间戳。

I add a form element something like this:

我添加了一个像这样的表单元素:

<input type="hidden" name="ie_sucks" id="ie_sucks", value="1" />

Then, in the function to submit the form via AJAX I set this hidden input to the current timestamp with something like this:

然后,在通过 AJAX 提交表单的函数中,我将此隐藏输入设置为当前时间戳,如下所示:

$('#ie_sucks').val(new Date().getTime());

The above code uses JQuery, so in pure JS it would be something like:

上面的代码使用了 JQuery,所以在纯 JS 中它会是这样的:

document.getElementById('ie_sucks').value = new Date().getTime();

This is not a pretty solution, but it does work.

这不是一个很好的解决方案,但它确实有效。

回答by David

I know jQuery's .ajax()call has a parameter called 'cache' which, if set to false, will force requested pages not to be cached by the browser. It's probably worth checking the jQuery sourceto see how they do it.

我知道 jQuery 的.ajax()调用有一个名为 'cache' 的参数,如果设置为 false,将强制浏览器不缓存请求的页面。可能值得检查一下 jQuery 源代码,看看他们是如何做到的。

(I'm checking it now and will update if I find anything, but posting this answer early in case you or anybody else has better luck finding it.)

(我现在正在检查它,如果我发现任何东西,我会更新,但请尽早发布此答案,以防您或其他任何人有更好的运气找到它。)