处理数据表中的会话超时(使用服务器端数据源处理)
我有一个由服务器端 Ajax 数据源支持的数据表表单(它在后端使用 struts 操作来处理请求、获取数据并发送 JSON 响应)。 服务器端操作需要在身份验证模式下运行,即需要有一个活动会话。
处理数据表中会话超时错误的最佳方法是什么?目前它只是显示 JSON 格式错误,这对于用户来说不是最佳选择。出于明显的原因(兼容性、未来的可维护性等),我不想去更改数据表代码。有没有一种巧妙的方法来处理错误?
我正在考虑在 JSON 响应中嵌入错误消息,但是在前端流程中拦截它的最佳位置在哪里?
编辑:我认为进行此类后处理的最佳位置是在 fnServerData 中,我对吗?
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.
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?
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?
EDIT: I presume the best place for doing such post processing would be in fnServerData, am I correct?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我会发回包含某种错误代码的 JSON 响应。要处理它,您需要像您猜测的那样定义 fnServerData 。但是,在使用错误回调之前,我会强烈考虑用例,原因如下:
错误只是获取资源的任何问题,并使用状态代码。假设会话在服务器上终止。用户请求数据,服务器返回500错误。错误回调说:“好吧,这很糟糕。让我们重定向到登录页面。”一切都很好。
然而......用户提出了请求。无论出于何种原因(大数据集、网络条件),该请求都会花费一点时间。与此同时,用户决定导航到另一个页面,从而中断呼叫响应循环。如果没有响应,错误回调将被触发,用户将被重定向到登录页面(或错误回调中设置的任何函数)。
问题是,我不知道“会话已过期”的状态代码(不过,我很想知道!我不介意在这里犯错!),以便捕获和处理。
我并不是说你“不应该”或“不能”使用错误回调。但该函数必须考虑会话过期以外的错误。您可能需要根据状态代码进行不同的处理。如果您的函数能够优雅地处理所有这些情况,那就太好了!在一个应用程序中,我们确实重定向到登录页面,并且错误回调通常会由于误报而出错,从而将用户错误地转储到登录页面。对于“会话已过期”的情况,我们通过 JSON 消息在成功回调中捕获它。
[在 Dave 的精彩评论后更新:] 如果您的服务器返回一个有用的服务器错误(401、403、550 或任何在您的场景中有意义的错误),则在 .ajax() 调用中使用 fnServerData 和 statusCode 参数(这太拗口了! )也会同样有效。我认为这是相同的工作量:通过您已经编写的方法返回 JSON,或者通过您应该已经有权访问的方法返回状态错误。选择对您有意义的一项。
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:
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.
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.
[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.
下面的技术对我有用......我知道响应已经太晚了,但希望代码能帮助别人
1)检查控制器(MVC)中的会话是否超时并返回
应该在客户端处理的状态代码
2) 在客户端使用 jQuery 全局 ajax Error,它捕获所有 ajax 调用的错误。检查状态代码并重定向到您的登录页面
3) [可选]禁用数据表中的警告并检查控制台中的错误
Below technique worked for me ... I know it was too late to respond but hoping the code will help someone
1) Check if session is timed out in the controller (MVC) and return the
status code which should be handled at the client side
2) At the client side use jQuery global ajax Error, which catches the errors from all the ajax calls. Check for the status code and redirect to your login page
3) [Optional] Disable the warnings from data tables and check the errors in the console
尽管这个问题已经被问了很长时间了,但如果有人需要的话,我还是会发布我的解决方案。
此解决方案适用于 JQuery Datatables 版本 1.10.16。
经过一番挖掘后,我发现了在会话过期时显示警报的功能。如下所示:
正如您所看到的,对类型变量进行检查以查看它是否是函数。如果是,它就会被调用。请注意,类型变量是这样获取的:
所以我这样做了:
只需将 YourErrorCode 替换为您需要的即可,它将起作用。
我承认这不是最干净的代码,但它确实有效。
Even though it has been a long time since this question has been asked i'm going to post my solution if just in case anybody needs it.
This solution applies to version 1.10.16 of JQuery Datatables.
After a bit of digging arround I found the function that shows the alert when the session expires. Here it is:
As you can see there is a check on the type variable to see if it's a function. If it is it gets invoked. Note that the type variable is obtained like this:
So i did this:
Just replace YourErrorCode with want you need and it will work.
I'll admit it's not the cleanest code ever but it works.
我已经使用比尔·马蒂诺的答案完成了这里:
http://datatables.net/forums /discussion/4377/catch-text-returned-from-server-side-ajax-call
在 Ajax 源响应中,您应该返回一个如下所示的 Json 字符串:
I have done it using the answer from bill martino here:
http://datatables.net/forums/discussion/4377/catch-text-returned-from-server-side-ajax-call
On the Ajax Source response you should return a Json string like this:
1.在获取ajax错误页面中使用此代码。或者也使用页脚文件。此代码在重定向到登录页面后在会话超时时工作,不会出现 ajax 错误。
2.单击在出现错误时调用函数的按钮..将一行放入函数中,
如一个示例
1.Using this code in your getting ajax error page. or also used footer file.this code working for at session timeout after redirect to login page not getting ajax error.
2.click on button called to function at time getting error..put the one line in function
like one example