如何在 .net MVC 4 中中止长 运行 Ajax 请求?
How to Abort Long running Ajax Request in .net MVC 4?
我找到了几个答案,包括 this one and also this one。但出于某种原因 this.Response.IsClientConnected
仍然是 true
。
这是我的客户:
<div class="ajax-loader-gif-close" id="cancelBusy">Close</div>
@using (Html.BeginForm("List", "Visit", FormMethod.Post, new { @id = "formFilter" }))
{
//Some models some TextBoxFor etc.
}
<script type="text/javascript" language="javascript">
var ajaxCallObject = null;
$(document).ready(function () {
$('form').submit(function () {
ajaxCallObject = $.ajax({
url: this.action,
type: this.method,
data: $(this).serialize(),
beforeSend: function () {
toggleBusyIndicator();
},
complete: function () {
toggleBusyIndicator();
},
success: function (result) {
//Some stuff
}
});
return false;
});
}
$("#cancelBusy").click(function () {
ajaxCallObject.abort();
});
});
</script>
这是我的服务器端:
[HttpPost]
public PartialViewResult List(VisitFilterViewModel model)
{
//IsClientConnected is ALWAYS TRUE even though I abort from client
while (this.Response.IsClientConnected)
{
Thread.Sleep(500);
}
return PartialView("PartialVisitFilterList", model);
}
使用 FireBug 我看到操作已中止,但 IsClientConnected
仍然是 true
。我做错了什么?
注意:如果您考虑删除 [HttpPost]
属性来解决此问题,我已经尝试过但没有成功。
感谢this post终于解决了!
实际上,我所做的完全有效,但由于我是 运行 在我的本地计算机上使用 VS Development,响应从未结束。但是,当我把它放在 IIS 服务器上时,一切正常。
万一站点或 post 被删除,我post在这里给出答案:
The Cancel button is working fine when the application is hosted on
IIS. If you are testing on the VS development server the
Response.IsClientConnected will always be true. Keep in mind that in
the previous versions of the RadProgressArea the cancellation is not
working under Chrome. For Q2.2014 this will be resolved. You could
test our internal build for this.
我找到了几个答案,包括 this one and also this one。但出于某种原因 this.Response.IsClientConnected
仍然是 true
。
这是我的客户:
<div class="ajax-loader-gif-close" id="cancelBusy">Close</div>
@using (Html.BeginForm("List", "Visit", FormMethod.Post, new { @id = "formFilter" }))
{
//Some models some TextBoxFor etc.
}
<script type="text/javascript" language="javascript">
var ajaxCallObject = null;
$(document).ready(function () {
$('form').submit(function () {
ajaxCallObject = $.ajax({
url: this.action,
type: this.method,
data: $(this).serialize(),
beforeSend: function () {
toggleBusyIndicator();
},
complete: function () {
toggleBusyIndicator();
},
success: function (result) {
//Some stuff
}
});
return false;
});
}
$("#cancelBusy").click(function () {
ajaxCallObject.abort();
});
});
</script>
这是我的服务器端:
[HttpPost]
public PartialViewResult List(VisitFilterViewModel model)
{
//IsClientConnected is ALWAYS TRUE even though I abort from client
while (this.Response.IsClientConnected)
{
Thread.Sleep(500);
}
return PartialView("PartialVisitFilterList", model);
}
使用 FireBug 我看到操作已中止,但 IsClientConnected
仍然是 true
。我做错了什么?
注意:如果您考虑删除 [HttpPost]
属性来解决此问题,我已经尝试过但没有成功。
感谢this post终于解决了! 实际上,我所做的完全有效,但由于我是 运行 在我的本地计算机上使用 VS Development,响应从未结束。但是,当我把它放在 IIS 服务器上时,一切正常。
万一站点或 post 被删除,我post在这里给出答案:
The Cancel button is working fine when the application is hosted on IIS. If you are testing on the VS development server the Response.IsClientConnected will always be true. Keep in mind that in the previous versions of the RadProgressArea the cancellation is not working under Chrome. For Q2.2014 this will be resolved. You could test our internal build for this.