Unexpected Caching of AJAX results in IE8

2019-01-05 08:27发布

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) { ... }
});

10条回答
Anthone
2楼-- · 2019-01-05 08:48

Just wrote a blog on this exact issue only using ExtJS (http://thecodeabode.blogspot.com/2010/10/cache-busting-ajax-requests-in-ie.html )

The problem was as I was using a specific url rewriting format I couldn't use conventional query string params (?param=value), so I had write the cache busting parameter as a posted variable instead..... I would have thought that using POST variables are a bit safer that GET, simply because a lot of MVC frameworks use the pattern

protocol://host/controller/action/param1/param2

and so the mapping of variable name to value is lost, and params are simply stacked... so when using a GET cache buster parameter

i.e. protocol://host/controller/action/param1/param2/no_cache122300201

no_cache122300201 can be mistaken for a $param3 parameter which could have a default value

i.e.

public function action($param1, $param2, $param3 = "default value") { //..// }

no chance of that happening with POSTED cache busters

查看更多
我命由我不由天
3楼-- · 2019-01-05 08:50

this is what i do for ajax calls:

var url = "/mypage.aspx";
// my other vars i want to add go here
url = url + "&sid=" + Math.random();
// make ajax call

it works pretty well for me.

查看更多
虎瘦雄心在
4楼-- · 2019-01-05 08:53

NickFitz gives a good answer, but you'll need to turn the caching off in IE9 as well. In order to target just IE8 and IE9 you could do this;

<!--[if lte IE 9]>
<script>
    $.ajaxSetup({
        cache: false
    });
</script>
<![endif]-->
查看更多
Summer. ? 凉城
5楼-- · 2019-01-05 08:57

IE is within its rights to do this caching; to ensure the item isn't cached, the headers should be set accordingly.

If you are using ASP.NET MVC, you can write an ActionFilter; in OnResultExecuted, check filterContext.HttpContext.Request.IsAjaxRequest(). If so, set the response's expire header: filterContext.HttpContext.Response.Expires = -1;

As per http://www.dashbay.com/2011/05/internet-explorer-caches-ajax/:

Some people prefer to use the Cache - Control: no - cache header instead of expires. Here’s the difference:
Cache-Control:no-cache – absolutely NO caching
Expires:-1 – the browser “usually” contacts the Web server for updates to that page via a conditional If-Modified-Since request. However, the page remains in the disk cache and is used in appropriate situations without contacting the remote Web server, such as when the BACK and FORWARD buttons are used to access the navigation history or when the browser is in offline mode.

查看更多
何必那么认真
6楼-- · 2019-01-05 08:59

Gets are always cacheable. One strategy that may work is to edit the response header and tell the client to not cache the information or to expire the cache very soon.

查看更多
姐就是有狂的资本
7楼-- · 2019-01-05 08:59

If you are using ASP.NET MVC, it is enough to add this line on top of the controller action:

[OutputCache(NoStore=true, Duration = 0, VaryByParam = "None")]
public ActionResult getSomething()
{

}
查看更多
登录 后发表回答