我正在尝试构建一个进度条,试图在发生回发时定期从客户端运行ajax调用。
然而,似乎ajax调用被阻止,可能是因为回发。回发完成后,带有ajax调用的http请求从挂起到取消。
问题:
网页aspx:
<%@ Page Language="C#" AutoEventWireup="true"
CodeBehind="TestProgressIndicator.aspx.cs"
Inherits="Test.TestProgressIndicator" %>
<!DOCTYPE html>
<html>
<head>
<script src="//code.jquery.com/jquery-3.2.1.min.js"
type="text/javascript"></script>
<script type="text/javascript">
function start_progress()
{
var indicator = $(".progress-indicator");
indicator.show();
var num_div = indicator.find(".num");
num_div.text("0%");
setTimeout(update_progress, 80);
function update_progress()
{
console.log("update_progress");
$.post(
"Ajax/ProgressBarCallback.aspx"
, function (progress)
{
num_div.text(progress + "%");
if (progress < 100)
setTimeout(update_progress, 80);
else
indicator.hide();
});
}
}
</script>
</head>
<body>
<form runat="server">
<asp:ScriptManager runat="server"/>
<div class="progress-indicator">
<div class="num"></div>
</div>
<asp:Button ID="btnStart" runat="server" Text="Starta"
OnClientClick="start_progress()" OnClick="btnStart_OnClick" />
</form>
</body>
</html>
的Ajax / ProgressBarCallback.aspx:
<%@ Page Language="C#" %>
<script runat="server">
void Page_Load(object sender, EventArgs e)
{
var progressObj = HttpContext.Current.Session["progress"];
var progress = progressObj != null ? int.Parse(progressObj.ToString()) : 0;
Response.ContentType = "text/json";
Response.Write(progress);
}
</script>
我知道这篇文章是ASP.NET making AJAX calls during long Postback的副本,但问了5年,它没有得到任何解释性答案。