如何中止长时间运行的Ajax请求的.NET MVC 4? [英] How to Abort Long running Ajax Request in .net MVC 4?

查看:150
本文介绍了如何中止长时间运行的Ajax请求的.NET MVC 4?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经发现了几个答案,包括<一href="http://stackoverflow.com/questions/8914728/how-do-you-cancel-an-ajax-long-running-mvc-action-client-side-in-javascript">this 之一,也是这一个。但由于某些原因 this.Response.IsClientConnected 却还在继续为

I've found several answers including this one and also this one. But for some reason this.Response.IsClientConnected still goes as 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);
}

使用萤火我看到这一行动被中止,但 IsClientConnected 还是。我究竟做错了什么?

With FireBug I see that the action is Aborted, but IsClientConnected is still true. What am I doing wrong?

请注意:如果你考虑删除 [HttpPost] 属性来解决这个问题,我已经尝试过,没有工作

Note: If you consider removing [HttpPost] attribute to solve this, I've already tried and didn't work.

推荐答案

感谢<一href="http://d585tldpucybw.cloudfront.net/forums/cancel-button-on-progressarea-to-stop-a-long-running-process"相对=nofollow>这个帖子我终于解决了! 其实,我所做的是完全工作,但因为我是在我的本地计算机VS开发运行此,响应从来没有结束。但是,当我把它的IIS服务器上,一切工作就好了。

Thanks to this post I finally solved it! Actually, what I did was completely working but since I was running this in my local computer with VS Development, the Response was never ended. But, when I put it on IIS Server, everything worked out just fine.

在案件现场或后期被删除时,我在这里张贴了答案:

In case site or the post is deleted, I'm posting the answer here:

取消按钮是否工作正常时,应用程序托管在   IIS。如果您正在测试的VS开发服务器上   Response.IsClientConnected永远是正确的。请记住,在   该RadProgressArea的previous版本取消不   Chrome浏览器下工作。对于Q2.2014这将得到解决。你可以   测试我们的内部版本这一点。

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.

这篇关于如何中止长时间运行的Ajax请求的.NET MVC 4?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆