Update
我发现这一定是缓存问题,但我无法关闭缓存。
这是我更改后的脚本:
<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 秒更新一次网页的一部分。它只在清除浏览器缓存后起作用一次,之后就不起作用了——每隔3秒向/Home/Refresh发出请求,从服务器发送数据,但网页上不显示任何内容;后续请求发送cookie ASP.NET_SessionId=wrkx1avgvzwozcn1frsrb2yh。
我正在使用 ASP.NET MVC 2 和 c#。
我的 jQuery 有问题,这是我的网络应用程序的工作原理
- Search.aspx 网页包含一个表单和 jQuery 脚本,在用户单击“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]
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秒。按下提交按钮后,上面的 jQuery 脚本调用
Home 控制器中的 Refresh() 操作。
public ActionResult Refresh()
{
ViewData["Result"] = DateTime.Now;
return PartialView();
}
问题在于,在 Internet Explorer 8 中,只有一个对 /Home/Refresh 的请求;
在 Firefox 3.6.3 中,所有对 /Home/Refresh 的请求都会发出,但网页上不会显示任何内容。 Firefox 的另一个问题是每秒都会发出对 /Home/Refresh 的请求,而不是每 5 秒发出一次。
我注意到,在清除 Firefox 缓存后,第一次按下 button1 时脚本运行良好,但之后就不起作用了。
如果有帮助的建议,我将不胜感激。