处理数据表中的会话超时(使用服务器端数据源处理) [英] Handling session time-outs in Datatables (with server-side datasource processing)

查看:96
本文介绍了处理数据表中的会话超时(使用服务器端数据源处理)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由服务器端Ajax数据源(在后端使用struts动作来处理请求,获取数据并发送JSON响应)支持的数据表形式. 服务器端操作需要以身份验证模式运行,即必须有一个活动会话.

I have a datatables form that's backed by a server-side Ajax data source (which uses a struts action in the backend to process the request, fetch the data and send a JSON response). The server-side operation needs to run in authenticated mode, i.e. there needs to be an active session.

处理数据表中会话超时错误的最佳方法是什么?目前,它仅显示JSON格式错误,这不是用户的最佳选择.我不想出于明显的原因(兼容性,将来的可维护性等)去更改数据表代码.有没有整齐的错误处理方法?

What is the best way to handle session timeout errors in Datatables? At present it just shows a JSON formatting error, which is not the best option for the user. I do not want to go and change the datatables code for obvious reasons (compatibility, future maintainability etc). Is there a neat way of handling errors?

我一直在考虑将错误消息嵌入JSON响应中的路线,但是在前端流中拦截它的最佳位置在哪里?

I was thinking along the lines of embedding error message in the JSON response but where is the best place to intercept it in the front-end flow?

我认为进行此类后处理的最佳位置是在fnServerData中,对吗?

I presume the best place for doing such post processing would be in fnServerData, am I correct?

推荐答案

我会回传包含某种错误代码的JSON响应.要处理它,您需要根据推测定义fnServerData.但是,出于这个原因,在使用错误回调之前,我会强烈考虑用例:

I would fire back a JSON response containing some sort of error code. To process it, you need to define fnServerData as you have surmised. However, I would strongly consider the use case before using the error callback for this reason:

错误只是获取资源的任何问题,并使用状态代码.假设会话在服务器上终止.用户请求数据,服务器发回500错误.错误回调说:好,糟透了.让我们重定向到登录页面."一切正常.

Error is simply any problem fetching the resource, and uses status codes. Let's say the session is terminated on the server. User requests the data, the server sends back a 500 error. Error callback says, "Well, that sucked. Let's redirect to a login page." Everything works great.

但是...用户发出请求.无论出于何种原因(大数据集,网络条件),该请求都会花费一点时间.同时,用户决定导航到另一个页面,从而中断呼叫响应循环.没有响应,错误回调将被触发,并且用户将被重定向到登录页面(或错误回调中设置的任何功能).

However... the user makes a request. The request is taking a little bit of time for whatever reason (large data set, network conditions). In the meantime, the user decides to navigate to another page, interrupting the call-response loop. With no response, the error callback is tripped and the user is redirected to a login page (or whatever function is set in the error callback).

问题是我没有知道的状态代码(尽管我很想知道!我不介意在这里错了!)的会话已过期"以便捕获和处理.

The problem is that there's no status code that I'm aware of (I'd love to know, though! I don't mind being wrong here!) for 'session has expired' in order to capture and process.

我并不是说您不应"或不能"使用错误回调.但是该功能必须考虑到会话期满以外的其他错误.您可能需要根据状态代码进行不同的处理.如果您的函数可以很好地处理所有这些情况,那就太好了!在一个应用程序中,我们确实是在重定向到登录页面,并且由于误报,错误回调通常会跳闸,从而将用户错误地转储到登录页面.对于会话过期",我们通过JSON消息将其捕获到成功回调中.

I'm not saying you "shouldn't" or "can't" use the error callback. But the function has to take into account errors other than the session expiring. You might need to get into processing differently based on status codes. If your function handles all those cases elegantly, great! In one application, we are indeed redirecting to login page, and the error callback would often trip due to a false positive, dumping the user incorrectly to the login page. For the case of "session expired" we are catching it in the success callback via a JSON message.

[在Dave的出色评论后更新:]如果服务器返回有用的服务器错误(401、403、550或您的情况下有意义的任何错误),请在.ajax()调用中将fnServerData与statusCode参数一起使用(即一口!)也一样有效.我认为这是相同的工作:通过您已经编写的方法返回JSON,或者通过您应该已经可以访问的方法返回状态错误.选择最适合您的一个.

[updated after Dave's excellent comments: ] If you have your server return a useful server error (401, 403, 550 or whatever makes sense in your scenario) then using fnServerData with statusCode parameter in the .ajax() call (that's a mouthful!) would work just as well. I think it's the same amount of work: return JSON via a method you've already written, or return status error via methods you should already have access to. Choose which one makes sense for you.

这篇关于处理数据表中的会话超时(使用服务器端数据源处理)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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