【问题标题】:Handling Ajax After Session Timeout会话超时后处理 Ajax
【发布时间】:2014-03-22 10:10:15
【问题描述】:

当会话超时时,浏览器请求的页面重定向到登录页面。我也有一些页面将 json 返回到 ajax 请求。现在,当会话超时时,这些页面可以返回类似 {sessiontimeout:1} 的内容,而不是它们应该返回的内容。但是,这意味着我必须在我的所有 ajax 响应函数中进行检查。或者我可以包装 ajax 函数来处理它:

success:function(result){
  if(result.sessiontimeout){
    window.location.href = login page;
  }else{realfunction(result)}
}

我的问题是,有没有更好的方法来做到这一点?

【问题讨论】:

  • 这样看起来不错。你还想要什么?

标签: php ajax session-timeout


【解决方案1】:

还有另一种方法是使用 jQuery PHP 库,我经常使用它,你可以在这里找到它http://code.google.com/p/jquery-php/downloads/list 以及如何使用它http://jquery.hohli.com/

这是它的工作原理。要检查会话是否关闭,然后使用像这样的 jQuery 插件重定向用户,在脚本文件中添加以下内容。

(function ($) {
    $.fn.jRedirect = function(location, time) {  
         return this.each(function() { 
            window.setTimeout ( function() {
           window.location = location; 
        },time*1000);
         });  
     } 
})(jQuery)

确保在页面上添加此内容:

<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<script src="YOUR_PATH.jquery.php.js" type="text/javascript"></script>
<script type="text/javascript" src="YOUR_PATH.jquery.form.js" ></script>

然后添加一个像这样的按钮:

<a onclick="$.php('ajax.php');"> Check user</a>

在 ajax.php 文件中你放了这个:

include(YOUR_PATH_TO.'jQuery.php'); // from the download

//Note: make sure you already declared session start and see if there's a session.

if($session_out){ // check if session is out then redirect 

jQuery('body')->jRedirect('login page');

}else{

jQuery('#message')->html('I'm already logged in');

}

jQuery::getResponse();

我希望你做对了,它是替代解决方案,并且 100% 有效。

【讨论】:

    【解决方案2】:

    如您所知,“ajax 请求的服务器响应”将检查是否返回任何基本值,例如如果 json 对象包含“id”键,则检查“id”是否存在。如果是,则意味着您的数据将重定向用户或刷新该页面。或显示正确的信息

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-11
      • 1970-01-01
      • 1970-01-01
      • 2017-12-30
      • 2013-06-10
      • 2013-01-20
      • 2010-11-03
      相关资源
      最近更新 更多