将 jQuery $.post() 中的缓存设置为 false?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5542701/
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-08-26 19:20:44  来源:igfitidea点击:

Setting the cache in jQuery $.post() to be false?

jquery

提问by AKor

I have this code:

我有这个代码:

$.post("php/tagNotifUnsub.php", $("#tagNotifUnsub").serialize(), function(){ 
            $('#tagSubDiv').load('tags.php #tagSubDiv', function(){$( "button, input:submit, input:button, a#jql, input:radio" ).button();});      
        });

I understand that $.post is just another name for $.ajax. And I know $.ajax has caching on by default. I want it to be uncached. How can I do that?

我知道 $.post 只是 $.ajax 的另一个名字。而且我知道 $.ajax 默认情况下有缓存。我希望它不被缓存。我怎样才能做到这一点?

回答by Daniel A. White

$.postis not cached.

$.post没有缓存。

Pages fetched with POST are never cached, so the cache and ifModified options in jQuery.ajaxSetup() have no effect on these requests.

使用 POST 获取的页面永远不会被缓存,因此 jQuery.ajaxSetup() 中的缓存和 ifModified 选项对这些请求没有影响。

From http://api.jquery.com/jQuery.post/

来自http://api.jquery.com/jQuery.post/

回答by Elias

Partly right, Daniel A. White!

部分正确,丹尼尔·A·怀特

On devices running iOS6 and Safari, POST is also cached.

在运行 iOS6 和 Safari 的设备上,POST 也会被缓存。

What you can do is the following:

您可以执行以下操作:

$.ajaxSetup({
   type: 'POST',
   headers: { "cache-control": "no-cache" }
});

回答by Stretch

You mention $.ajax

你提到 $.ajax

You can turn caching off for ajax like this:

您可以像这样关闭 ajax 的缓存:

$.ajax({
        url: "/myURL?",
        cache: false,
    });

回答by Sergei Nester

This is an old problem that seems to pop up from time to time and I am always scratching my head. This solution fixes the problem but it may not be suitable in all cases as it is in effect disabling a key feature of AJAX - The asynchronous feature. That said and done for the cases where I have had this problem - Apple products and Safari I'm looking at you especially- setting the additional parameter of async:falsein your jQuery AJAX request fixes the issue and actually solves the "No Alert on Post Callback" problem.

这是一个似乎不时出现的老问题,我总是在挠头。此解决方案解决了该问题,但它可能并不适用于所有情况,因为它实际上禁用了 AJAX 的一个关键功能 - 异步功能。对于我遇到此问题的情况-Apple 产品和 Safari 我特别关注您-async:false在您的 jQuery AJAX 请求中设置附加参数可修复该问题并实际解决了“无后回调警报”的问题问题。

As I said if you absolutely rely on asynchronous functionality then this is not going to help you, but for many projects it will stop you pulling your hair out.

正如我所说,如果您绝对依赖异步功能,那么这对您没有帮助,但对于许多项目,它会阻止您拔毛。

$.ajax({
  type: 'POST',
  url: url,
  data: data,
  success: success,
  dataType: dataType,
  async:false
});