Skip to content Skip to sidebar Skip to footer

Force Page Refresh On Redirect

I am working on some bug fixes for a old Zend Framework 1.10 project and I am having some issues with redirection and page refresh. The problem: make an AJAX call, check if the per

Solution 1:

If you delete_attr is the URL that makes the call to PersonController->deleteAction() your problem is that your browser does an Ajax request (which is redirected) but the returned data is getting back to your ajax call (success: function (result) {) and the page the user is currenltly viewing is the same.

What you can do is add a new variable (do_redirect => true) and the url you want to redirect your user to, and if your success function get that url - it should do the redirect:

classPersonControllerextendsZend_Controller_Action{
    ...
    // this will delete the person from DB and will make a redirection to indexActionpublicfunctiondeleteAction()
    {
        if ($this->getRequest()->isPost()) {
            ...
            $person->deletePerson($person_id);

            return$this->_helper->json(
                ['do_redirect' => TRUE, 'redirect_url' => 'index.php']
            );
        }
    }
}

And in your ajax call you should check for do_redirect:

$.ajax({
    url: delete_attr,
    data: {'id': id},
    success: function (result) {
        if ('do_redirect'in result && 'redirect_url'in result) {
            window.location.href = result.redirect_url
        }
        if (result.count > 0) {
            alert('You can not delete this person. Try deleting associated insurances first.')
        } else {
            $.ajax({
                url: href_attr,
                data: {'id': id},
                type: 'POST',
                success: function(result) {
                    if ('do_redirect'in result && 'redirect_url'in result) {
                        window.location.href = result.redirect_url
                    }
                }
            });
        }
    }
});

Post a Comment for "Force Page Refresh On Redirect"