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

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

问题描述

我在从一个jQuery Ajax请求的Internet Explorer缓存结果一个严重的问题。

我在我的网页,获取每个用户浏览到一个新的页面时更新了头。一旦在页面加载我这样做

  $。获得(/游戏/ getpuzzleinfo,空,功能(数据,状态){
    VAR内容=< H1>维基百科迷宫LT; / H1>中;
    内容+ =&其中,P级='endtopic'>寻找<跨度><一个标题='打开你正在寻找一个单独的标签或窗口中的主题HREF ='+ data.EndTopicUrl + 目标='_空白'>中+ data.EndTopic +&其中;一个/>&所述; /跨度>&所述; / P>中;
    内容+ =&其中,P级='步'→步骤<跨度>中+ data.StepCount +&所述; /跨度>&所述; / P>中;
    内容+ =&其中,P级='级别'>级<跨度>中+ data.PuzzleLevel.toString()+&所述; /跨度>&所述; / P>中;
    含量+ =&其中,P类='startover'>&所述; A HREF ='/游戏/启动/+ data.PuzzleId.toString()+'>重新开始与所述; / a取代;&所述; / P> ;

    $(#wikiheader)追加(内容);

},JSON);
 

这只是注入的头信息到页面中。你可以检查它通过将 www.wikipediamaze.com ,然后在登录和启动一个新的难题。

在每一个浏览器我测试过(谷歌浏览器,火狐,Safari,IE浏览器),它的伟大工程的除了在IE浏览器。 Eveything被注入只是罚款,IE浏览器的第一次,但在那之后它甚至从来没有使呼叫 /游戏/ getpuzzleinfo 。这就像它已缓存的结果或什么的。

如果我改变调用 $。员额(/游戏/ getpuzzleinfo,... IE浏览器捡起来就好了。但随后火狐停止工作。

有人可以提供一些线索在此,为什么IE浏览器缓存我的 $。获得 Ajax调用?

更新

每下面的建议,我已经改变了我的Ajax请求到这一点,这固定我的问题:

  $。阿贾克斯({
    键入:GET,
    网址:/游戏/ getpuzzleinfo
    数据类型:JSON,
    缓存:假的,
    成功:功能(数据){...}
});
 

解决方案

IE是臭名昭著的激​​进Ajax响应的缓存。当你使用jQuery,您可以设置一个全局选项:

  $。ajaxSetup({
    缓存:假的
});
 

这将导致jQuery来一个随机值添加到请求的查询字符串,从而$ ​​P $ pventing IE从缓存响应。

请注意,如果您有其他的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.

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

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