在重定向时强制刷新页面 [英] Force page refresh on redirect

查看:215
本文介绍了在重定向时强制刷新页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为一个旧的Zend Framework 1.10项目修复一些错误,并且在重定向和页面刷新方面遇到了一些问题.

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.

问题:拨打AJAX电话,检查人员是否已分配保险,如果分配了保险,则不允许删除该人,直到没有保险为止.

The problem: make an AJAX call, check if the person has insurance assigned and if so do not allow to delete the person until it does not have any insurances.

解决方案:

控制器: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

The 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刷新页面,该行就会消失,因为该行已被删除,这应该是正确的行为.

The issue: the code above works fine but has a gap, when deleteAction() is called and person gets deleted it tries a redirection to indexAction() there I am still seeing the deleted person and I shouldn't. As soon as I refresh the page using F5 or CTRL+R the row disappear because it was deleted and this should be the right behavior.

问题:我的解决方案有什么问题?在进行第二次AJAX调用时,是否有任何方法可以强制从控制器或jQuery代码刷新页面?

The question: what is wrong in my solution? is there any way to force page refresh either from the controller or from the jQuery code when the second AJAX call is made?

推荐答案

如果您delete_attr是进行PersonController->deleteAction()调用的URL,则问题是您的浏览器执行了Ajax请求(已重定向),但是返回的数据将返回到您的Ajax调用(success: function (result) {),并且用户当前正在查看的页面是相同的.

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.

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

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:

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:

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

这篇关于在重定向时强制刷新页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆