在 IE8 中意外缓存 AJAX 结果 [英] Unexpected Caching of AJAX results in IE8

查看:23
本文介绍了在 IE8 中意外缓存 AJAX 结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Internet Explorer 缓存来自 JQuery Ajax 请求的结果时遇到了严重问题.

我的网页上有标题,每次用户导航到新页面时都会更新.加载页面后,我执行此操作

$.get("/game/getpuzzleinfo", null, function(data, status) {var content = "

维基百科迷宫

";content += "<p class='endtopic'>Looking for <span><a title='在单独的选项卡或窗口中打开您正在查找的主题' href='" + data.EndTopicUrl + "'目标='_blank'>"+ data.EndTopic + "<a/></span></p>";content += "

Step "+ data.StepCount + "</span></p>";content += "

Level "+ data.PuzzleLevel.toString() + "</span></p>";content += "<p class='startover'><a href='/game/start/" + data.PuzzleId.toString() + "'>重新开始</a></p>";$("#wikiheader").append(content);}, "json");

它只是将标题信息注入页面.您可以通过访问 www.wikipediamaze.com 然后登录并开始新拼图来查看.>

在我测试过的每个浏览器(Google Chrome、Firefox、Safari、Internet Explorer)中,它运行良好除了在 IE 中.一切都在 IE 中第一次被很好地注入,但之后它甚至从未调用过/game/getpuzzleinfo.好像缓存了结果什么的.

如果我将调用更改为 $.post("/game/getpuzzleinfo", ... IE 会很好地接收它.但随后 Firefox 停止工作.

有人能解释一下为什么 IE 缓存我的 $.get ajax 调用吗?

更新

根据下面的建议,我已将我的 ajax 请求更改为此,解决了我的问题:

$.ajax({类型:获取",url: "/game/getpuzzleinfo",数据类型:json",缓存:假,成功:函数(数据){ ... }});

解决方案

IE 因其对 Ajax 响应的积极缓存而臭名昭著.当您使用 jQuery 时,您可以设置一个全局选项:

$.ajaxSetup({缓存:假});

这将导致 jQuery 向请求查询字符串添加一个随机值,从而阻止 IE 缓存响应.

请注意,如果您在需要缓存的地方有其他 Ajax 调用,这也会为这些调用禁用它.在这种情况下,切换到使用 $.ajax() 方法并为必要的请求显式启用该选项.

参见 http://docs.jquery.com/Ajax/jQuery.ajaxSetup了解更多信息.

I'm having a serious issue with Internet Explorer caching results from a JQuery Ajax request.

I have header on my web page that gets updated every time a user navigates to a new page. Once the page is loaded I do this

$.get("/game/getpuzzleinfo", null, function(data, status) {
    var content = "<h1>Wikipedia Maze</h1>";
    content += "<p class='endtopic'>Looking for <span><a title='Opens the topic you are looking for in a separate tab or window' href='" + data.EndTopicUrl + "' target='_blank'>" + data.EndTopic + "<a/></span></p>";
    content += "<p class='step'>Step <span>" + data.StepCount + "</span></p>";
    content += "<p class='level'>Level <span>" + data.PuzzleLevel.toString() + "</span></p>";
    content += "<p class='startover'><a href='/game/start/" + data.PuzzleId.toString() + "'>Start Over</a></p>";

    $("#wikiheader").append(content);

}, "json");

It just injects header info into the page. You can check it out by going to www.wikipediamaze.com and then logging in and starting a new puzzle.

In every browser I've tested (Google Chrome, Firefox, Safari, Internet Explorer) it works great except in IE. Eveything gets injected just fine in IE the first time but after that it never even makes the call to /game/getpuzzleinfo. It's like it has cached the results or something.

If I change the call to $.post("/game/getpuzzleinfo", ... IE picks it up just fine. But then Firefox quits working.

Can someone please shed some light on this as to why IE is caching my $.get ajax calls?

UPDATE

Per the suggestion below, I've changed my ajax request to this, which fixed my problem:

$.ajax({
    type: "GET",
    url: "/game/getpuzzleinfo",
    dataType: "json",
    cache: false,
    success: function(data) { ... }
});

解决方案

IE is notorious for its aggressive caching of Ajax responses. As you're using jQuery, you can set a global option:

$.ajaxSetup({
    cache: false
});

which will cause jQuery to add a random value to the request query string, thereby preventing IE from caching the response.

Note that if you have other Ajax calls going on where you do want caching, this will disable it for those too. In that case, switch to using the $.ajax() method and enable that option explicitly for the necessary requests.

See http://docs.jquery.com/Ajax/jQuery.ajaxSetup for more info.

这篇关于在 IE8 中意外缓存 AJAX 结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆