2014-01-28 76 views
0

其实我使用Ajax调用我的代码示例在这里..请不要介意语法如何强制页面重新加载在Zend的PHP

$.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响应..有没有办法去做吧?

回答

0

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

它可以通过简单的JS代码来实现

window.location.reload(); 
+0

是的,从前端我们可以做到,但我只是想从后端做..没有办法做到这一点吗? – Dhivya

0

当会话过期只是适当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请求。

相关问题