ASP发生了什么。当用户在收到响应之前离开时,NET MVC控制器?

时间:2022-12-04 16:43:04

I have an AJAX action that can take a couple of minutes to complete depending upon the amount of data involved. If a user gets frustrated and navigates away while this action is still running, what happens to the controller? Does it complete anyway? Does it know the request should be abandoned and dispose of the controller object?

我有一个AJAX操作,根据所涉及的数据量,可以花几分钟完成。如果用户感到沮丧并在此操作仍在运行时导航,那么控制器会发生什么情况?它完整呢?它知道应该放弃请求并处理控制器对象吗?

1 个解决方案

#1


8  

It will not cancel the request to the server as the act of navigating away does not send any information back to the server regarding that request. The client (browser), however, will stop listening for it. After the request finishes, regardless if the client was listening for it or not, the controller will dispose as it typically would.

它不会取消对服务器的请求,因为导航离开不会向服务器发送关于该请求的任何信息。然而,客户端(浏览器)将停止监听它。在请求完成之后,无论客户机是否监听,控制器将按照通常的方式处理。

With that said, you could get fancy and use a combination of listening for a page change on the client side and calling abort on the AJAX request to the server.

说到这里,您可以使用一种组合,即监听客户端上的页面更改,并调用对服务器的AJAX请求的abort。

This SO question discusses how to abort a request. I could imagine setting a variable when you first start the AJAX request and then unsetting it when it finished.

这个问题讨论了如何中止请求。我可以想象在第一次启动AJAX请求时设置一个变量,然后在它完成时取消设置。

Warning - Pseudo code below

警告-下面的伪代码

var isProcessing = false;

var xhr = $.ajax({
    type: "POST",
    url: "myUrl",
    beforeSend: function(){
       isProcessing = true;
    }
    complete: function(){
       isProcessing = false;
    }
});

window.onbeforeunload = function(){
   if(isProcessing){
       xhr.abort();
   }
}

The above is very basic idea of the concept, but there should probably be some checks around if the xhr object exists, perhaps also bind/unbind the window.onbeforeunload in the beforeSend and complete object handlers for the .ajax() item.

上面的内容是概念的基本思想,但是如果xhr对象存在,那么应该进行一些检查,也许还需要绑定/解绑定窗口。onbeforeunload在前面提到的.ajax()项的完整对象处理程序中。

#1


8  

It will not cancel the request to the server as the act of navigating away does not send any information back to the server regarding that request. The client (browser), however, will stop listening for it. After the request finishes, regardless if the client was listening for it or not, the controller will dispose as it typically would.

它不会取消对服务器的请求,因为导航离开不会向服务器发送关于该请求的任何信息。然而,客户端(浏览器)将停止监听它。在请求完成之后,无论客户机是否监听,控制器将按照通常的方式处理。

With that said, you could get fancy and use a combination of listening for a page change on the client side and calling abort on the AJAX request to the server.

说到这里,您可以使用一种组合,即监听客户端上的页面更改,并调用对服务器的AJAX请求的abort。

This SO question discusses how to abort a request. I could imagine setting a variable when you first start the AJAX request and then unsetting it when it finished.

这个问题讨论了如何中止请求。我可以想象在第一次启动AJAX请求时设置一个变量,然后在它完成时取消设置。

Warning - Pseudo code below

警告-下面的伪代码

var isProcessing = false;

var xhr = $.ajax({
    type: "POST",
    url: "myUrl",
    beforeSend: function(){
       isProcessing = true;
    }
    complete: function(){
       isProcessing = false;
    }
});

window.onbeforeunload = function(){
   if(isProcessing){
       xhr.abort();
   }
}

The above is very basic idea of the concept, but there should probably be some checks around if the xhr object exists, perhaps also bind/unbind the window.onbeforeunload in the beforeSend and complete object handlers for the .ajax() item.

上面的内容是概念的基本思想,但是如果xhr对象存在,那么应该进行一些检查,也许还需要绑定/解绑定窗口。onbeforeunload在前面提到的.ajax()项的完整对象处理程序中。