代码之家  ›  专栏  ›  技术社区  ›  bojo

CakePHP+jQuery AJAX回调-不通过$session->flash()删除会话;

  •  2
  • bojo  · 技术社区  · 16 年前

    基本上是这样的:

    • 访问应用程序时会加载主模板页。
    • 链接由onclick处理程序处理,onclick处理程序通过jqueryajax调用重新加载主机的相关部分,触发其他事件等。
    • 初始onclick处理程序完成后,它将调用两个或更多ajax回调,这些回调将更新页面的其他部分。

    问题是:

    • 最后一个回调读取CakePHP的flash消息命令$session->flash(),并在一个简单的javascript弹出窗口中显示结果,谁的div标记是通过$('body').prepend()创建的。关闭弹出窗口后,创建的元素将从DOM中删除。
    • 当用户单击一个链接并为下一页运行flash消息回调时,CakePHP flash消息数据从未被删除,它将重新显示消息。当用户继续单击页面时,这将在任何地方重复2-5次,或者直到过了一段时间,最后从PHP会话中删除消息。
    • 直接访问ajax链接时,flash数据显示一次,然后在重新加载页面后删除。

    代码非常简单,如下所示:

    <?php echo $content_for_layout ?>
    
    <script>
      $(window).ready(function() {
        page_load_callback();
      });
    </script>
    

    function page_load_callback() {
    
      $.ajaxSetup({
            cache: true // We want our client to cache stuff, and override this in PHP.
      });
    
      $.ajax({
            cache: false, // Force this to not cache via jQuery
            beforeSend: function() {
                $('body').prepend('<div id="flash_message_popup"></div>');
            },
            url: flash_message_popup_link, // global variable set in default.ctp
            success: function(data) {
                $('#flash_message_popup').html(data);
                if ($('#flash_message_popup').is(':empty')) {
                    $('#flash_message_popup').remove();
                } else {
                    create_popup('flash_message_popup');
                }
            },
            error: function(a, b, c) {
                $('#flash_message_popup').remove();
            }
        });
    }
    
    function create_popup(tag) {
        if (editing_hall == true) {
            return false;
        }
        var selector = '#' + tag;
        $(selector).attr('style', 'position: absolute; display: none; top: 5; left: 5; z-index: 100;');
        $(selector).slideDown('slow');
    }
    

    *.ctp文件。

    <?php if ($message = $session->flash()): ?>
    <span id="flash_data">
        <div class="flash_message"><?php echo $message; ?></div>
        <div class="btn_large bottom_round"><a name="" onclick="$('#flash_message_popup').slideUp('slow', function() { $('#flash_message_popup').remove(); });">close</a></div>
    </span>
    <?php endif; ?>
    

    控制器方法。

    function flash_message_popup() {
        $expires = 60*60*24*30*7;
        header('Expires: '.gmdate('D, d M Y H:i:s', time()-$expires) . ' JST'); // minus for past  
        $this->layout = 'ajax';
    
        $this->render('../elements/flash_message_popup');
    }
    

    如果有人有什么建议,我很乐意听听。我没有主意了。

    编辑

    2 回复  |  直到 16 年前
        1
  •  0
  •   Leo    16 年前

    我认为现在的情况是,AJAX在PHP重置会话flash之前就开始使用它了。通常,在CRUD方法中设置为成功或失败,例如。

        if ($this->Invoice->save($this->data)) {
            $this->Session->setFlash(__('The Invoice has been saved', true));
            $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(__('The Invoice could not be saved. Please, try again.', true));
        }
    

        2
  •  0
  •   bojo    16 年前

    问题似乎是太多的AJAX请求与数据库会话冲突,因此计时完全中断,数据库数据似乎被旧的会话数据覆盖。我就到此为止,结束这个问题。