How to fix browser cache and notmodified respond for JSON? jQuery.ajax({ifModified:true,cache:true}) JSON request break on data respond

别来无恙 提交于 2019-12-01 21:14:09

I believe this is how it is supposed to work a 304 doesn't return any data it just tells you it hasn't changed.

However, I do see the problem if you haven't got the data already in memory then you need some method to get it from the browsers cache. Therefore I think the solution is the write code to cache the data.

I am not sure how HTTPS works with etags, however, HTTPs data isn't always cached (different methods and behaviour between browsers and versions) so if etags work you may need to implement your own secure cache anyway.

Try adding a random number to the end of your url as a param.

random_number = Math.floor(Math.random()*10101010101)
url:'http://localhost/api?' + random_number

When you receive a 304 you must re-request the data but with the "ifModified" flag set to false. The request will then be subject to normal caching rules and you will receive your cached data.

For instance, in an MVC controller...

        DateTime pageLastUpdated = <.....>

        if (Request.Headers["If-Modified-Since"] != null)
        {
            var dt = DateTime.Parse(Request.Headers["If-Modified-Since"] as string);

            if (pageLastUpdated.Date == dt.Date && pageLastUpdated.Hour == dt.Hour && pageLastUpdated.Minute == dt.Minute && pageLastUpdated.Second == dt.Second) {
              Response.Cache.SetCacheability(HttpCacheability.NoCache);                    
              return new HttpStatusCodeResult(304, "notmodified");
            }
        }

        Response.Cache.SetCacheability(HttpCacheability.Private);
        Response.Cache.SetVaryByCustom("*");
        Response.Cache.SetExpires(pageLastUpdated.AddDays(1));
        Response.Cache.SetLastModified(pageLastUpdated);

        // now return the Json
        return Json(new {........});

The data sent back is cached on the client for up to 1 day.

function loadJson(url, params, onLoaded) {
  // initial request 
  $.ajax({
    type: 'GET',
    dataType: 'json',
    url: url,
    data: params,
    cache: true,
    ifModified: true, // forces check with server
    success: function (result, textStatus, jqXHR) {

        // if 304, re-request the data
        if (result === undefined && textStatus == 'notmodified') {
            $.ajax({
                type: 'GET',
                dataType: 'json',
                url: url,
                data: params,
                cache: true,
                ifModified: false, // don't check with server
                success: function (cachedResult, textStatus, jqXHR) {
                    onLoaded(cachedResult);
                }
            });
        }
        else
            onLoaded(result);
    }
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!