EXT JS 会话超时

发布于 2024-10-31 18:28:57 字数 66 浏览 1 评论 0原文

EXT JS - 我想知道如何检查会话超时的 json 响应,例如用户是否空闲了 20 分钟左右,如果他的会话是否过期

EXT JS - I would like to know how to check the json response for a session time out like if a user is idle for say 20 minutes or so if his session is expired or not

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜清冷一曲。 2024-11-07 18:28:57

ExtJS 中没有处理会话超时的标准方法。 ExtJS 是一个客户端库,用于创建应用程序的用户界面/前端层,而会话管理则在服务器端进行。

ExtJS Ajax 请求实现了回调机制。这意味着将某个 Javascript 函数指定为回调函数,当 Ajax 请求完成(成功或失败)时调用该函数。以下是取自 ExtJS API 文档 的示例 - 查看参数成功和定义回调函数的失败:

// Basic request
Ext.Ajax.request({
   url: 'foo.php',
   success: someFn,
   failure: otherFn,
   headers: {
       'my-header': 'foo'
   },
   params: { foo: 'bar' }
});

因此,在会话超时的情况下,您可以(例如)构造一个 JSON 响应,其中包含一些错误代码(由您定义)以及要向用户显示的错误消息。然后,回调函数应该检查服务器是否返回此错误,并在发生这种情况时采取必要的操作(显示错误消息、重定向到登录页面等)。

请注意,在上述情况下,从 ExtJS 的角度来看,Ajax 请求实际上会成功。当 HTTP 请求完全失败(如 403 等 HTTP 错误)时,Ajax 请求被视为不成功。这很重要,因为通常可以为成功和不成功的请求定义不同的回调函数(如上面的示例代码所示)。

There is no standard way of handling session timeouts in ExtJS. ExtJS is a client-side library, used to create the user interface/front-end layer of an application, while session management takes place on the server side.

ExtJS Ajax requests implement a callback mechanism. It means that a certain Javascript function is assigned as the callback function, which is called when the Ajax request has finished (either successfully or unsuccessfully). Here's an example taken from ExtJS API Documentation - see parameters success and failure that define the callback functions:

// Basic request
Ext.Ajax.request({
   url: 'foo.php',
   success: someFn,
   failure: otherFn,
   headers: {
       'my-header': 'foo'
   },
   params: { foo: 'bar' }
});

So, in the case of session timeout, you could (for example) construct a JSON response, which would contain some error code (defined by you), and an error message to be shown to the user. The callback function should then check if this error is returned from the server, and take necessary actions (show error message, redirect to login page, etc.) when that happens.

Note that in the above case, from ExtJS viewpoint, the Ajax request would actually be successful. When the HTTP request fails altogether (HTTP errors like 403 and such), the Ajax request is considered unsuccessful. This is important because it is usually possible to define different callback functions for successful and unsuccessful requests (as in the above sample code).

ヅ她的身影、若隐若现 2024-11-07 18:28:57

您可以模拟超时会话...

 var keepaliveHandler = new Ext.util.DelayedTask(function(){
    Ext.Ajax.request({
        url : '/keepalive',
        method : 'GET',
        success: function(response, options){
//dummy server call each 60 seconds
            keepaliveHandler.delay(60000);
        }
    });
});
var timeoutHandler = new Ext.util.DelayedTask(function(){
//invalidate session
    Ext.Ajax.request({
        url : '/logout',
        method : 'GET',
        success: function(response, options){
            Ext.MessageBox.show({
                title: MessagesMap.getMessage('session.closed'),
                msg: MessagesMap.getMessage('session.closed.message'),
                buttons: Ext.MessageBox.OK,
                fn: function() {
                    window.location.pathname = '/';                 
                },
                icon: Ext.MessageBox.WARNING
            });
        }
    });
});
if(Ext.ux.SystemProperties.isLogged) {
    keepaliveHandler.delay(60000);
    timeoutHandler.delay(Ext.ux.SystemProperties.timeout);
//check for mouse movements
    document.body.onmousemove = function(e) {
        timeoutHandler.delay(Ext.ux.SystemProperties.timeout);
    };  
}

You can mock the timeout session...

 var keepaliveHandler = new Ext.util.DelayedTask(function(){
    Ext.Ajax.request({
        url : '/keepalive',
        method : 'GET',
        success: function(response, options){
//dummy server call each 60 seconds
            keepaliveHandler.delay(60000);
        }
    });
});
var timeoutHandler = new Ext.util.DelayedTask(function(){
//invalidate session
    Ext.Ajax.request({
        url : '/logout',
        method : 'GET',
        success: function(response, options){
            Ext.MessageBox.show({
                title: MessagesMap.getMessage('session.closed'),
                msg: MessagesMap.getMessage('session.closed.message'),
                buttons: Ext.MessageBox.OK,
                fn: function() {
                    window.location.pathname = '/';                 
                },
                icon: Ext.MessageBox.WARNING
            });
        }
    });
});
if(Ext.ux.SystemProperties.isLogged) {
    keepaliveHandler.delay(60000);
    timeoutHandler.delay(Ext.ux.SystemProperties.timeout);
//check for mouse movements
    document.body.onmousemove = function(e) {
        timeoutHandler.delay(Ext.ux.SystemProperties.timeout);
    };  
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文