无法禁用jQuery缓存

时间:2010-05-25 22:16:47

标签: jquery caching asp.net-mvc-2

更新

我发现它一定是缓存问题,但我无法关闭缓存。 这是我改变的脚本:

<script src="../../Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script type="text/javascript">
        jQuery.ajaxSetup({
            // Disable caching of AJAX responses 
            cache: false
        });

        jQuery("#button1").click(function (e) {
            window.setInterval(refreshResult, 3000);
        });

        function refreshResult()
        {
            jQuery("#divResult").load("/Home/Refresh");
        }
</script>

它每3秒更新一次网页的一部分。清除Web浏览器缓存后,它只工作一次,之后它不起作用 - 请求/ Home / Refresh没有间隔3秒,数据从服务器发送,但网页上没有显示任何内容;后续请求发送cookie ASP.NET_SessionId = wrkx1avgvzwozcn1frsrb2yh。 我正在使用ASP.NET MVC 2和c#。




我遇到了jQuery的问题,这是我的网络应用程序的工作原理

  • 在用户点击button1按钮后,包含表单和jQuery脚本的Search.aspx网页将数据发布到Home控制器中的Search()操作。

Search.aspx:

<%@ Page Title="" Language="C#" 
    MasterPageFile="~/Views/Shared/Site.Master" 
    Inherits="System.Web.Mvc.ViewPage<GLSChecker.Models.WebGLSQuery>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Title  
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <h2>Search</h2>

    <% Html.EnableClientValidation(); %>

    <% using (Html.BeginForm()) {%>    
        <fieldset>
            <div class="editor-label">
                <%: Html.LabelFor(model => model.Url) %>
            </div>
            <div class="editor-field">
                <%: Html.TextBoxFor(model => model.Url, 
                    new { size = "50" } ) %>
                <%: Html.ValidationMessageFor(model => model.Url) %>
            </div>

            <div class="editor-label">
                <%: Html.LabelFor(model => model.Location) %>
            </div>
            <div class="editor-field">
                <%: Html.TextBoxFor(model => model.Location, 
                    new { size = "50" } ) %>
                <%: Html.ValidationMessageFor(model => model.Location) %>
            </div>

            <div class="editor-label">
                <%: Html.LabelFor(model => model.KeywordLines) %>
            </div>
            <div class="editor-field">
                <%: Html.TextAreaFor(model => model.KeywordLines, 10, 60, null)%>  
                <%: Html.ValidationMessageFor(model => model.KeywordLines)%>
            </div>

            <p>
                <input id ="button1" type="submit" value="Search" />
            </p>
        </fieldset>

    <% } %>

    <script src="../../Scripts/jquery-1.4.1.js" type="text/javascript"></script>
    <script type="text/javascript">
        jQuery("#button1").click(function (e) {
            window.setInterval(refreshResult, 5000);
        });

        function refreshResult()
        {
            jQuery("#divResult").load("/Home/Refresh");
        }
    </script>

   <div id="divResult"> 
   </div>

</asp:Content>
        [HttpPost]
        public ActionResult Search(WebGLSQuery queryToCreate)
        {
            if (!ModelState.IsValid)
                return View("Search");

            queryToCreate.Remote_Address = HttpContext.Request.ServerVariables["REMOTE_ADDR"];
            Session["Result"] = null;

            SearchKeywordLines(queryToCreate);

            Thread.Sleep(15000);

            return View("Search");
        }//Search()
  • 点击button1按钮后,Search.aspx网页上的脚本就会运行。

  • 控制器中的Search()动作运行的时间较长。我模拟 这是在测试中通过放置Thread.Sleep(15000);在Search()动作中。

  • 5秒按下Submit按钮后,上面的jQuery脚本调用 Home Controller中的Refresh()动作。

        public ActionResult Refresh()
        {               
            ViewData["Result"] = DateTime.Now;

            return PartialView();
        }
  • Refresh()呈现此部分
  

&lt;%@ Control Language =“C#”   继承= “System.Web.Mvc.ViewUserControl”   %GT;

&lt;%= ViewData [“结果”]%&gt;

问题是在Internet Explorer 8中只有一个请求/ Home / Refresh; 在Firefox 3.6.3中,所有对/ Home / Refresh的请求都已生成,但网页上没有显示任何内容。 Firefox的另一个问题是/ Home / Refresh的请求每隔一秒发送一次,而不是每5秒一次。
我注意到,在我清除Firefox缓存后,第一次按下button1时脚本运行良好,但之后它不起作用。

我很感激有用的建议。

5 个答案:

答案 0 :(得分:3)

现在这是一个古老的问题。但是我遇到了一个我认为与缓存相关的问题,但事实证明这是另一回事,我认为某些人可能会觉得它很有用。

我有一套类似这样的例程

  1. MAIN PAGE通过.load调用SUB PAGE。 SUB PAGE有数据输入表 以及用于启动SQL PAGE以执行相关sql的超链接

  2. 数据输入SUB PAGE后 (通过MAIN PAGE),点击超链接 使用SQL PAGE更新数据库,然后刷新 主页div包含SUB PAGE

  3. 嗯......几个小时我一直在输入数据,然后点击相关链接才发现MAIN PAGE div有时会刷新,有时却没有。 F5总是有用。所以我很确定我有缓存问题。所以我停止使用.load并开始使用.ajax,使用'cache:false'。我可以看到.ajax现在适当地随机化了获取url,但仍然我的MAIN PAGE div并不令人耳目一新。我确定我有一个缓存问题......

    但我没有。

    使用谷歌Chrome的开发工具 - 我可以看到,有时我的jQuery例程正在完成它应该做的事情,即

    a)点击SUB PAGE;执行SQL PAGE;刷新MAIN PAGE div

    有时事情按此顺序发生......

    b)单击SUB PAGE;刷新MAIN PAGE div;执行SQL PAGE

    我的代码是为了做a),但由于SQL PAGE需要一段时间才能执行,b)实际上是发生了什么。并且b)很糟糕 - 我的MAIN PAGE div实际上是刷新的,但更新我的数据表的SQL在刷新之前没有执行。

    一旦我意识到这一点,修复很简单。使用.ajax,我实现了以下逻辑:

    i)单击SUB PAGE ii)执行SQL PAGE iii)有条件成功执行SQL PAGE(使用.ajax中的'success:'选项),然后刷新包含SUB PAGE的MAIN PAGE div iv)我保留了'cache:false',以防万一

    此修复程序100%用于我的代码

    因此,如果您想要解决为什么缓存无法关闭,请确保您确实存在缓存问题,而不是我上面描述的问题。

    祝你好运

答案 1 :(得分:1)

您可以通过在GET参数中添加时间戳来更改您请求的网址:

function refreshResult()
{
  jQuery("#divResult").load("/Home/Refresh?"+(+new Date()));
}

这应忽略浏览器缓存(因为url每次都在更改)。当然 - 这就是在ajax请求上将jQuery cache参数设置为false的情况,我不是100%确定.load()将使用该设置。

答案 2 :(得分:1)

任何浏览器ajax处理。从未缓存过。

// Global Vars
var ajaxRefreshCount = 0;
var ajaxRequestsCount = 0;

// Call this function for the ajax request
function ajaxRequest(name)
{
    ajaxRefreshCount++;
    ajaxRequestsCount++;
    var reqAjax = jQuery.get("/?ajax=" + name + "&r="+ajaxRefreshCount+"&s="+new Date().getTime(), function (data) { ajaxCallback(data, name)} );
}

function ajaxCallback(data, ajaxRequestName)
{
    ajaxRequestsCount--;

        // Your Stuff
        if (ajaxRequestName == "home_refresh")
            jQuery("#divResult").html(data);

    if (ajaxRequestsCount == 0) ajaxFullyLoaded();
}

function ajaxFullyLoaded()
{
    // Execute when all ajax requests where processed
}

// Example for home refresh
ajaxRequest("home_refresh");

答案 3 :(得分:0)

IE8问题可能是IE缓存了类似的请求,但是如果他们处理这个问题我不熟悉JQueries内部,解决方案可能是添加一个随机的get var。

答案 4 :(得分:-1)

我找到了解决方案,我换了

<% using (Html.BeginForm()) {%>

<% using (Ajax.BeginForm(null)) {%>
相关问题