我试图获取长时间运行的状态进度。它在 FF:)、Opera 和 IE:? 中运行良好,但在 Chrome 中?!它失败了:它永远不会进入通用处理程序中的 ProcessRequest。拜托,你能建议我做错了什么吗?
默认.aspx:
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="AspNetProgressBar._Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js" type="text/javascript"></script>
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<asp:Button runat="server" Text="Run" OnClick="button_Click" OnClientClick="button_start()" />
...
<script type="text/javascript">
function button_start() {
setInterval(function () {
$.ajax({
url: 'StatusHandler.ashx',
data: { statusKeyId: '<%= ViewState["key"] %>' },
type: 'post',
dataType: 'json',
success: function (json) {
... process response
},
error: function () {
}
});
}, 1000);
}
</script>
...
默认.aspx.cs
....
protected void button_Click(object sender, EventArgs e)
{
System.Threading.Thread.Sleep(2000);
for (int i = 0; i < 10; ++i)
{
System.Threading.Thread.Sleep(500);
// TODO: update progress status
}
// lblStatus.Text = "Done";
}
状态处理程序.ashx.cs:
public class StatusHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "application/json";
string statusKeyId = context.Request.Params["statusKeyId"];
if (statusKeyId != null)
{
System.Web.Script.Serialization.JavaScriptSerializer s = new System.Web.Script.Serialization.JavaScriptSerializer(GetStatusInfo(statusKeyId ));
string output = s.Serialize(info);
context.Response.Write(output);
return;
}
context.Response.Write("{test: \"empty\"}");
}