jQuery $.getJSON 在 IE8 中返回缓存数据

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

$.getJSON returning cached data in IE8

jqueryasp.net-mvcinternet-explorer-8getjson

提问by Andrew Harry

I'm playing around with ASP.net MVC and JQuery at the moment. I've come across behavour which doesn't seem to make sense.

我现在正在玩 ASP.net MVC 和 JQuery。我遇到了似乎没有意义的行为。

I'm calling JQuery's $.getJSONfunction to populate some div's. The event is triggered on the $(document).readyevent. This works perfectly.

我正在调用 JQuery 的$.getJSON函数来填充一些 div。该事件是在事件上触发的$(document).ready。这完美地工作。

There is a small AJAX.BeginFormwhich adds another value to be used when populating the divs. It calls the remote function correctly and upon success calls the original javascript function to repopulate the divs.

有一个 smallAJAX.BeginForm在填充 div 时添加了另一个要使用的值。它正确调用远程函数,成功后调用原始 javascript 函数来重新填充 div。

Here is the weird part: In FireFox and Chrome - Everything works. BUT In IE8 (Beta) this second call to the populate Div script (which calls the $.getJSON function) gets cached data and does not ask the server!

这是奇怪的部分:在 FireFox 和 Chrome 中 - 一切正常。但是在 IE8 (Beta) 中,第二次调用填充 Div 脚本(调用 $.getJSON 函数)获取缓存数据并且不询问服务器!

Hope this question makes sense: In a nut shell - Why is $.getJSONgetting cached data? And why is it only effecting IE8?

希望这个问题有意义:简而言之 - 为什么要$.getJSON获取缓存数据?为什么它只影响 IE8?

采纳答案by Nico

Just to let you know, Firefox and Chrome consider all Ajax request as non-cachable. IE (all versions) treat Ajax call just as other web request. That's why you see this behavior.
How to force IE to download data at each request:

只是让您知道,Firefox 和 Chrome 将所有 Ajax 请求视为不可缓存的。IE(所有版本)将 Ajax 调用视为其他 Web 请求。这就是为什么你会看到这种行为。
如何强制 IE 在每次请求时下载数据:

  • As you said, use 'cache' or 'nocache' option in JQuery
  • Add a random parameter to the request (ugly, but works :))
  • On server side, set cachability (for example using an attribute, see below)
  • 正如你所说,在 JQuery 中使用 'cache' 或 'nocache' 选项
  • 向请求添加一个随机参数(丑陋,但有效:))
  • 在服务器端,设置可缓存性(例如使用属性,见下文)

Code:

代码:

public class NoCacheAttribute : ActionFilterAttribute
{
    public override void OnActionExecuted(ActionExecutedContext context)
    {
        context.HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
    }
}

回答by Jitesh Patil

This is how it worked for me...

这就是它对我的工作方式......

$.ajaxSetup({ cache: false });
$.getJSON("/MyQueryUrl",function(data,item) {
     // do stuff with callback data
     $.ajaxSetup({ cache: true });
   });

回答by Andrew Harry

Thanks Kent for your answer. Using $.ajax('{ cache: no }'); worked perfectly. [edit]

感谢肯特的回答。使用 $.ajax('{ cache: no }'); 完美地工作。[编辑]

Or at least I thought i did. Seems that the jquery $.getJSON isn't reading any changes made to the $.ajax object.

或者至少我认为我做到了。似乎 jquery $.getJSON 没有读取对 $.ajax 对象所做的任何更改。

The solution that ended up working was to add a new parameter manually

最终工作的解决方案是手动添加一个新参数

var noCache = Date();
$.getJSON("/somepage/someaction", { "noCache": noCache }, Callback);

the date resolution is only to the minute; which effectively means this solution still caches for upto one minute. This is acceptable for my purposes.

日期分辨率仅为分钟;这实际上意味着此解决方案仍会缓存长达一分钟。这对我的目的来说是可以接受的。

回答by Guy

I solved this same problem by placing the following attribute on the Action in the Controller:

我通过在控制器中的 Action 上放置以下属性解决了同样的问题:

[OutputCache(Duration = 0, VaryByParam = "None")]

回答by Josh

If you're using ASP.net MVC, consider adding an extension method to easily implement no caching like so:

如果您使用的是 ASP.net MVC,请考虑添加一个扩展方法来轻松实现无缓存,如下所示:

    public static void NoCache(this HttpResponse Response)
    {
        Response.Cache.SetNoStore();
        Response.Cache.SetExpires(DateTime.MinValue);
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.Cache.SetValidUntilExpires(false);

        Response.Expires = -1;
        Response.ExpiresAbsolute = DateTime.MinValue;
        Response.AddHeader("Cache-Control", "no-cache");
        Response.AddHeader("Pragma", "no-cache");
    }

回答by stadja

Ready for THE answer ?

准备好答案了吗?

http://lestopher.tumblr.com/post/21742012438/if-youre-using-ie8-and-getjson

http://lestopher.tumblr.com/post/21742012438/if-youre-using-ie8-and-getjson

So, just add

所以,只需添加

jQuery.support.cors = true;  

at the beginning of your script and BANG it works !

在您的脚本和 BANG 的开头,它起作用了!

回答by Kent Fredric

You may need to send a cache-breaker.

您可能需要发送缓存破坏者。

I would recommend using $.ajax( { cache: no }) just in case ( adds a random suffix to the get request)

我建议使用 $.ajax( { cache: no }) 以防万一(向 get 请求添加随机后缀)

( I tend to use $.ajax everywhere these days, more tuneable )

(这些天我倾向于在任何地方使用 $.ajax,更可调)