在每次 Jquery Ajax 命中之前检查 Session
我已经在 MVC 中实现了一个项目,并且我已经使用 ajax 请求完成了大部分工作。但我们面临会话超时的问题。所以我想用 ajax 做一些事情,在执行任何 ajax 请求之前检查会话,然后处理该 ajax 请求。
我不想在使用ajax的所有页面中编写if else条件。
请建议一些方法。
谢谢 阿图尔
I have implemented a project in MVC and there i have done most of the thing with ajax request. but we are facing a issue of session time out. So i want some thing with ajax that before doing any ajax request it check the session and then process that ajax request.
I do't want to write if else condition in all the page where i uses the ajax.
Please suggest some way.
Thanks
Atul
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
也许你应该检查 jQuery.ajaxPrefilter() ( http://api.jquery.com/jQuery.ajaxPrefilter/ )。另外,您不能修改有关空闲超时的应用程序池设置来完全避免会话超时吗?
Maybe you should check jQuery.ajaxPrefilter() ( http://api.jquery.com/jQuery.ajaxPrefilter/ ). Also can't you modify your Application Pool settings regarding Idle Time-out to avoid the session timeout altogether?
我今天已经解决了这个问题,这是我的解决方案:
这个 php 部分(文件或控制器方法)将由 ajax 调用:
现在,将此脚本放在所有具有 ajax 调用并需要用户的页面的开头要记录的:
global: false
部分在这里至关重要,以防止此 ajax 调用进入ajaxSend
并因此创建无限循环。I've been around this issue today and this is my solution:
This php part (file or controller method) will be called by ajax:
Now, put this script in the beginning of all of your pages that have ajax calls and require the user to be logged:
The
global: false
part is essential here, to prevent this ajax call to enter theajaxSend
and therefore create an infinite loop.我在项目中完成的一种方法是实现一个保持活动的ajax请求,该请求以一定的时间间隔在后台发送......这样你的会话就不会超时......
也许不是一种非常优雅的方式但它有效:)
One way I've done it in a project was to implement a keep-alive ajax request which gets sent in the background at a certain interval... so your session doesn't get timed out...
Maybe not a very elegant way but it worked :)
您可以创建一个 全局过滤器;全局过滤器影响每个控制器操作,可以检查会话是否存在,然后重定向到登录页面或执行某些操作。此外,您还可以通过创建一个使用 AJAX 执行 ping 操作的视图或一个 iframe 来保持会话处于活动状态,以保持对服务器的请求继续进行,从而使会话保持活动状态。问题是你想让会话保持那么长时间吗......
You could create a global filter; a global filter affects every controller action, can check whether session exists, and then redirect to the login page or do something in accordance. Also, you can keep the session alive instead by creating a view that you ping with AJAX or an iframe that keeps the requests to the server going, so the session stays alive. The question is do you want session to stay alive that long then...
我对 jQuery-ajax、spring mvc 和 spring security 架构也有同样的问题。
我遵循的解决方案如下。
如果发生会话超时,Spring Security 会将请求重定向到登录表单控制器。
在登录控制器中检查请求(保存的请求)是否是ajax请求(存在标头
x-requested-with
),如果是ajax请求,则返回指示超时的json/xml值。例如:{“会话超时”:true}
。然后使用全局 jQuery ajax 事件(如 ajaxSuccess/ajaxError)测试此 ajax 返回值,以查看是否存在会话超时并采取您想要采取的操作。
您可以使用您正在使用的任何应用程序框架尝试类似的操作。
I had the same problem with jQuery-ajax, spring mvc and spring security architecture.
The solution I followed was as follows.
If a session timeout occurs spring security will redirect the request to the login form controller.
In the login controller check whether the request(Saved Request) is an ajax request(presents of header
x-requested-with
), if it is an ajax request return an json/xml value indicating the timeout. ex:{"session-timeout" : true}
.Then test this ajax returned value using an global jQuery ajax event like ajaxSuccess/ajaxError to see whether there was an session timeout and take what ever actions you want to take.
You can try something like this using what ever application framework you are using.