【发布时间】:2011-02-23 22:26:21
【问题描述】:
更新
我发现它一定是缓存问题,但我无法关闭缓存。 这是我修改后的脚本:
<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 秒更新一次网页的一部分。清除浏览器缓存后只运行一次,之后就失效了——请求/Home/Refresh没有3秒的间隔,从服务器发送数据,但网页上什么都没有显示;后续请求发送 cookie ASP.NET_SessionId=wrkx1avgvzwozcn1frsrb2yh。 我正在使用 ASP.NET MVC 2 和 c#。
我的 jQuery 有问题,这是我的网络应用程序的工作方式
- 包含表单和 jQuery 脚本的 Search.aspx 网页在用户单击 button1 按钮后将数据发布到 Home 控制器中的 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]
公共 ActionResult 搜索(WebGLSQuery queryToCreate)
{
if (!ModelState.IsValid)
返回视图(“搜索”);
queryToCreate.Remote_Address = HttpContext.Request.ServerVariables["REMOTE_ADDR"];
会话[“结果”] = null;
SearchKeywordLines(queryToCreate);
线程.睡眠(15000);
返回视图(“搜索”);
}//搜索()
点击 button1 按钮后,Search.aspx 网页中的上述脚本运行。
控制器中的 Search() 操作运行时间更长。我模拟 通过放置 Thread.Sleep(15000) 进行测试;在 Search() 操作中。
5 秒。按下提交按钮后,上面的 jQuery 脚本调用 Home 控制器中的 Refresh() 操作。
- Refresh() 渲染这个部分
问题是在 Internet Explorer 8 中只有一个对 /Home/Refresh 的请求;
在 Firefox 3.6.3 中,所有对 /Home/Refresh 的请求都已发出,但网页上不显示任何内容。 Firefox 的另一个问题是对 /Home/Refresh 的请求是每秒而不是每 5 秒发出的。
我注意到,在我清除 Firefox 缓存后,脚本在第一次按下 button1 时运行良好,但在那之后它就不起作用了。
如果您有有用的建议,我将不胜感激。
【问题讨论】:
标签: jquery caching asp.net-mvc-2