0

Actually I am using ajax call and my code sample is here.. Please don't mind the syntax

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

Like this I am loading the response text into one div tag. Now my problem is in zend controller I am trying to check for one condition like if session expired means redirecting to login page or else sending the response html code for that action.

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');
    }
} 

I tried these three ways

$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)

I just want to reload the page. Just like triggering the browser refresh button to achieve what I want.. Please suggest any idea to solve my problem..

Note : I want to page reload from server side rather than checking in ajax response.. Is there a way to do it?

2 Answers 2

0

As you mentioned, you are trying to trigger "browser refresh", which is a frontend event. So I don't there you can do that in the backend.

It can be achieved using simple js code

window.location.reload();
1
  • Yes from front end we can do it but I just wanted it to do from back end.. isn't there a way to do it?
    – Dhivya
    Jan 29, 2014 at 4:44
0

When session expires just throw exception with proper HTTP code 401.

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

Then you can write global callback function for ajaxError and reload page or redirect user where you want.

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

You can take this one step further and write ACL plugin on preDispatch function to throw this exception. Then just ajdust little bit ErrorController so users are also redirected to login page and you will have consistent behavior for all requests, not only for XHR requests.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.