简体   繁体   English

如何在Angular JS中保持会话活跃

[英]How to keep session alive in angular JS

I am working on web application in which we have angularJS as UI and java as back end. 我正在开发将angularJS作为UI和java作为后端的Web应用程序。 We have configured tomcat server session timeout to 20 minutes. 我们已将tomcat服务器会话超时配置为20分钟。 AngularJS is responsible for fetching and saving data. AngularJS负责获取和保存数据。 When the data size is huge sometime it takes more than 20 minutes to process the request which is fine as per our business requirement. 如果某个时候的数据量很大,则需要20多分钟来处理该请求,这根据我们的业务要求是可以的。

We are facing issue if any ajax request takes more than 20 minutes. 如果任何ajax请求需要20分钟以上,我们将面临问题。 Whenever any operation takes more than 20 minutes session goes timeout however angularJS still waits for data to be returned. 每当任何操作花费超过20分钟的时间时,会话就会超时,但是angularJS仍然等待数据返回。 ajax request to server is not able to keep session alive. 对服务器的ajax请求无法使会话保持活动状态。 We checked it by opening a new tab in browser it asks for login where first tab waits for data to be returned. 我们通过在浏览器中打开一个新选项卡进行检查,它要求登录,第一个选项卡等待数据返回。

Is there any way to keep the session alive till the server processing the request. 在服务器处理请求之前,有什么方法可以使会话保持活动状态。

Below is my angularJS code to make request to server.B 以下是我的向服务器提出请求的angularJS代码

return $http({
            method: method,
            url: url,
            async:true,
            data: $.param(data),
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
                'X-Requested-With':'XMLHttpRequest'
            }
        }).then(function(response){
           if(response.status ===200){
                // do  some work
           }               else{
               failureCallBack(response.data, response.status, response);
           }
        });

You can configure angular timeout: 您可以配置角度超时:

From $http docs: timeout – {number|Promise} – timeout in milliseconds, or promise that should abort the request when resolved. 来自$ http docs:超时– {number | Promise} –超时(以毫秒为单位),或承诺解决后应中止请求。

Use angular keep alive 使用角度保持生命

$scope.$on('Keepalive', function() {
    // do something to keep the user's session alive});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM