$.getJSON returning cached data in IE8

前端 未结 7 1402
隐瞒了意图╮
隐瞒了意图╮ 2020-11-29 16:38

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.

I\'m calling JQuery\'s $.getJSON

相关标签:
7条回答
  • 2020-11-29 16:59

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

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

    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.

    0 讨论(0)
  • 2020-11-29 17:01

    This is how it worked for me...

    $.ajaxSetup({ cache: false });
    $.getJSON("/MyQueryUrl",function(data,item) {
         // do stuff with callback data
         $.ajaxSetup({ cache: true });
       });
    
    0 讨论(0)
  • 2020-11-29 17:02

    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)

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

    0 讨论(0)
  • 2020-11-29 17:03

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

    [OutputCache(Duration = 0, VaryByParam = "None")]
    
    0 讨论(0)
  • 2020-11-29 17:05

    Ready for THE answer ?

    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 !

    0 讨论(0)
  • 2020-11-29 17:12

    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:

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

    Code:

    public class NoCacheAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuted(ActionExecutedContext context)
        {
            context.HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
        }
    }
    
    0 讨论(0)
提交回复
热议问题