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.

我正在调用JQuery的$.getJSON函数来填充一些div.该事件在$(document).ready事件上触发.这个很好用.

有一个很小的AJAX.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!

希望这个问题是有意义的:在一个坚果壳里,为什么$.getJSON会得到缓存数据?为什么它只影响IE8?

推荐答案

为了让你知道,Firefox和Chrome将所有Ajax请求视为不可缓存的.IE(所有版本)将Ajax调用视为其他web请求.这就是为什么你会看到这种行为

  • 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)

代码:

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

Jquery相关问答推荐

在shiny 的datatable列中启用智能搜索

如何在jQuery中移动表格行?

在 contenteditable div 中的插入符号处插入 html

为什么要两次声明 jQuery?

如何通过 Select 插件使用 jQuery 验证?

如何使用 jquery 正确格式化货币?

jQuery .load() 调用不会在加载的 HTML 文件中执行 JavaScript

将 CSS 应用于 jQuery 对话框按钮

jQuery Force 为 iframe 设置 src 属性

使用 jQuery 获取选定的选项 id

如何使用jquery获取点击链接的href?

用 Javascript 加载 jQuery 并使用 jQuery

JavaScript:让代码每分钟运行一次

Bootstrap 4 文件输入

Isotope 和 Masonry jQuery 插件之间的区别

带有回调ajax json的jQuery自动完成

变量首字母大写

如何在调用 Ajax 等异步调用时让代码等待

jquery $(window).height() 正在返回文档高度

如何在 jQuery 中取消绑定悬停?