How to Abort Long running Ajax Request in .net MVC 4?

眉间皱痕 提交于 2019-12-24 17:08:05

问题


I've found several answers including this one and also this one. But for some reason this.Response.IsClientConnected still goes as true.

Here is my Client:

<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>

Here is my Server-Side:

[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);
}

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

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


回答1:


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:

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.



来源:https://stackoverflow.com/questions/29235850/how-to-abort-long-running-ajax-request-in-net-mvc-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!