【问题标题】:How to force page reload in zend php如何在zend php中强制页面重新加载
【发布时间】:2014-01-28 06:21:29
【问题描述】:

其实我用的是ajax调用,我的代码示例在这里..请不要介意语法

$.ajax(
    url: "mysite/action1",
success: function(resp) {
$("#divId").html(resp);
});

像这样我将响应文本加载到一个 div 标签中。现在我的问题出在 zend 控制器中,我正在尝试检查一种情况,例如会话是否过期意味着重定向到登录页面或发送该操作的响应 html 代码。

public function action1Action() {
    $login = new Zend_Session_Namespace('login');
    if (isset($login->employee_id)) {
        echo $html;

    } else{
        $this->view->headMeta()->appendHttpEquiv('refresh','1');
        //$this->_helper->redirector->gotoUrl($this->view->baseUrl());
        //$this->_helper->redirector('index', 'index');
    }
} 

这三种方法我都试过了

$this->view->headMeta()->appendHttpEquiv('refresh','1'); -- Nothing happening
$this->_helper->redirector->gotoUrl($this->view->baseUrl()); and $this->_helper->redirector('index', 'index'); -- loads login page within the div tag (as it is taking as the ajax response)

我只想重新加载页面。就像触发浏览器刷新按钮来实现我想要的一样..请提出任何解决我问题的想法..

注意:我想从服务器端重新加载页面,而不是检查 ajax 响应。有没有办法做到这一点?

【问题讨论】:

    标签: php ajax zend-framework


    【解决方案1】:

    正如您所提到的,您正试图触发“浏览器刷新”,这是一个前端事件。所以我不在那里你可以在后端做到这一点。

    用简单的js代码就可以实现

    window.location.reload();
    

    【讨论】:

    • 是的,我们可以从前端做到这一点,但我只是想从后端做到这一点。难道没有办法吗?
    【解决方案2】:

    当会话到期时,只需使用正确的HTTP code 401 抛出异常。

    throw new Zend_Controller_Action_Exception('User not authorized', 401);
    

    然后您可以为ajaxError编写全局回调函数并重新加载页面或将用户重定向到您想要的位置。

    $( document ).ajaxError(function(event, jqxhr) {
      if (jqxhr.status === 401) {
        window.location =  '/'; // Use same base url here
      } 
    });
    

    您可以更进一步,在 preDispatch 函数上编写 ACL 插件来抛出此异常。然后只需调整一点点 ErrorController,这样用户也将被重定向到登录页面,您将对所有请求都有一致的行为,而不仅仅是 XHR 请求。

    【讨论】:

      猜你喜欢
      • 2016-02-11
      • 2016-02-27
      • 1970-01-01
      • 1970-01-01
      • 2011-02-17
      • 1970-01-01
      • 2012-03-26
      • 2011-07-15
      • 1970-01-01
      相关资源
      最近更新 更多