强制重定向页面刷新

时间:2016-09-04 14:47:20

标签: javascript php jquery ajax zend-framework

我正在为旧的Zend Framework 1.10项目修复一些bug,我在重定向和页面刷新方面遇到了一些问题。

问题:进行AJAX通话,检查此人是否已分配保险,如果是,则在没有任何保险之前不允许删除此人。

解决方案:

控制器:crud/application/controllers/personController.php

class PersonController extends Zend_Controller_Action
{
    // this will fetch all the persons from DB and send to the view 
    public function indexAction()
    {
        $persons = new Application_Model_DbTable_Person();
        $this->view->persons = $persons->fetchAll();
    }

    // this will check whether the person has or not insurances
    public function hasinsurancesAction()
    {
        $hasInsurances = new Application_Model_DbTable_Person();

        return $this->_helper->json(
            ['count' => count($hasInsurances->personHasInsurances($this->_getParam('id')))]
        );
    }

    ...

    // this will delete the person from DB and will make a redirection to indexAction    
    public function deleteAction()
    {
        if ($this->getRequest()->isPost()) {
            $person_id = (int) $this->getRequest()->getPost('id');

            $person = new Application_Model_DbTable_Person();
            $person->deletePerson($person_id);

            $this->_helper->redirector('index');
        }
    }
}

视图:crud/application/views/scripts/company/index.phtml

<table>
    <tr>
        <th>Title</th>
        <th>&nbsp;</th>
    </tr>
    <?php foreach ($this->persons as $person) : ?>
        <tr>
            <td><?php echo $this->escape($person->title); ?></td>
            <td>
                <a href="<?php echo $this->url(
                    [
                        'controller' => 'person',
                        'action'     => 'edit',
                        'id'         => $person->id,
                    ]
                ); ?>">Edit</a>
                <a class="delete"
                   data-id="<?php echo $person->id ?>"
                   data-href="<?php echo $this->url(
                       [
                           'controller' => 'person',
                           'action'     => 'delete',
                           'id'         => $person->id,
                       ]
                   ); ?>"

                   data-delete-href="<?php echo $this->url(
                       [
                           'controller' => 'person',
                           'action'     => 'hasInsurances',
                       ]
                   ); ?>"
                   href="#">Delete</a>
            </td>
        </tr>
    <?php endforeach; ?>
</table>

Javascript / jQuery:crud/public/js/delete.js

$(function () {
    $('.delete').on('click', function () {
        id = $(this).data('id');
        href_attr = $(this).data('href');
        delete_attr = $(this).data('delete-href');

        $.ajax({
            url: delete_attr,
            data: {'id': id},
            success: function (result) {
                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'
                    });
                }
            }
        });
    })
});

问题:上面的代码工作正常但有差距,当调用deleteAction()并且有人被删除时,它尝试重定向到indexAction()我仍然看到被删除的人和我不应该。只要我使用F5CTRL+R刷新页面,该行就会消失,因为它已被删除,这应该是正确的行为。

问题:我的解决方案有什么问题?有没有办法在第二次AJAX调用时从控制器或jQuery代码强制页面刷新?

1 个答案:

答案 0 :(得分:1)

如果delete_attr是调用PersonController->deleteAction()的URL,那么您的浏览器会执行Ajax请求(重定向),但返回的数据将返回到您的ajax调用( success: function (result) {)和用户当前正在查看的页面是相同的。

您可以做的是添加一个新变量(do_redirect => true)和您要将用户重定向到的网址,如果您的成功函数获得该网址,则应该执行重定向:

class PersonController extends Zend_Controller_Action {
    ...
    // this will delete the person from DB and will make a redirection to indexAction
    public function deleteAction()
    {
        if ($this->getRequest()->isPost()) {
            ...
            $person->deletePerson($person_id);

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

在你的ajax调用中你应该检查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
                    }
                }
            });
        }
    }
});