Unable to disable jQuery cache

Update

I realized that this is a caching problem, but I cannot disable the cache. Here is my modified script:

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

It updates part of the web page every 3 seconds. It works only once after clearing the web browser cache, after which it does not work - requests are executed in / Home / Refresh without an interval of 3 seconds, data is sent from the server, but nothing is displayed on the web page; subsequent requests send cookie ASP.NET_SessionId = wrkx1avgvzwozcn1frsrb2yh. I am using ASP.NET MVC 2 and C #.




I have a problem with jQuery, this is how my web application works

  • The Search.aspx web page containing the form and the jQuery script publish the data for the Search () action in the Home Controller after the user clicks button1.

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()
  • 1 - script - Search.aspx.

  • Search() . , Thread.Sleep(15000); Search().

  • 5 . "" jQuery script () .

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

            return PartialView();
        }
  • Refresh()

<% @ = "#" Inherits = "System.Web.Mvc.ViewUserControl" % >

<% = ViewData [ "" ]% >

, Internet Explorer 8 /Home/Refresh; Firefox 3.6.3 /Home/Refresh , - . Firefox , /Home/Refresh 5 .
, , Firefox, script 1, .

.

+3
5

,

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

<% using (Ajax.BeginForm(null)) {%>
-1

. , , , , - , , - - .

, :

  • SUB PAGE .load. SUB PAGE SQL PAGE sql

  • SUB PAGE ( ), SQL PAGE, MAIN PAGE div, SUB PAGE

... , , , MAIN PAGE , . F5 . , . .load .ajax, "cache: false". , .ajax URL- get, MAIN PAGE div . , ...

.

Google Chrome, , - jQuery , , ..

a) SUB PAGE; SQL PAGE; div

...

b) SUB PAGE; MAIN PAGE div; SQL PAGE

), - , SQL PAGE , ) , . b) - MAIN PAGE div , SQL .

, . .ajax, :

i) SUB PAGE ii) SQL PAGE iii) SQL PAGE ( "success:" .ajax), div MAIN PAGE, SUB PAGE iv) "cache: false" ,

100%

, , , , , , , .

.

+3

URL-, , GET:

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

( URL- ). , , jQuery cache false ajax-, 100% , .load() .

+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");
+1

IE8 , IE , JQueries, , get var.

0
source

Source: https://habr.com/ru/post/1747097/


All Articles