zoukankan      html  css  js  c++  java
  • 关于IE的Ajax请求结果缓存的问题

    原文出处(详细):http://www.cnblogs.com/artech/archive/2013/01/03/cache-4-ie.html

    解决方法1:通过为URL地址添加后缀的方式解决问题

     <!DOCTYPE html>
     <html>
         <head>        
                <script type="text/javascript">
                    $(function () {
                        window.setInterval(function () {
                            $.ajax({
                                url:'@Url.Action("GetCurrentTime")?'+ new Date().toTimeString() ,
                                success: function (result) {
                                   $("ul").append("<li>" + result + "</li>");
                               }
                           });
                       }, 5000);
                   });
               </script>
           </head>
      </html>

    解决方法2:通过jQuery的Ajax设置解决问题

    <!DOCTYPE html>
        <html>
            <head>        
                <script type="text/javascript">
                    $(function () {
                        $.ajaxSetup({ cache: false }); 
                        window.setInterval(function () {
                            $.ajax({
                                url:'@Url.Action("GetCurrentTime")',
                               success: function (result) {
                                   $("ul").append("<li>" + result + "</li>");
                               }
                           });
                       }, 5000);
                   });
               </script>
           </head>
       </html>

    解决方法3:适用MVC   通过定制响应解决问题

     public class HomeController : Controller
        {
            public ActionResult Index()
            {
                return View();
            }
         
            [NoCache] 
            public string GetCurrentTime()
           {
               return DateTime.Now.ToLongTimeString();
           }
       }
       public class NoCacheAttribute : FilterAttribute, IActionFilter
       {
           public void OnActionExecuted(ActionExecutedContext filterContext)
           {
               filterContext.HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
           }
        
           public void OnActionExecuting(ActionExecutingContext filterContext)
           {}
       }
  • 相关阅读:
    2018年7月10日笔记
    2018年7月7日笔记
    2018年7月5日笔记
    2018年7月3日笔记
    sed 命令详解
    《软件构架实践》阅读笔记01
    《掌握需求过程》阅读笔记06
    《掌握需求过程》阅读笔记05
    第十二周进度条
    《掌握需求过程》阅读笔记04
  • 原文地址:https://www.cnblogs.com/xmily/p/3065121.html
Copyright © 2011-2022 走看看