在Cakephp 3中刷新后,Cookies消失了

时间:2018-01-13 17:22:52

标签: javascript php jquery cakephp cookies

这是我的HTML:

<a class="dropdown-item" value="1">Try1</a>
<a class="dropdown-item" value="2">Try2</a>
<a class="dropdown-item" value="3">Try3</a>

让我们来看看我的jQuery和ajax:

<script type="text/javascript">

$(document).ready(function() {

    $(".dropdown-item").click(function(){
         idString = $(this).attr("value");
        alert(idString);
        $.ajax({
            type: "POST",
            url: "<?php echo  \Cake\Routing\Router::url(array('controller' => 'MyController', 'action' => 'index')); ?>",
            data: "idString="+idString,
            success: function(result){

                $("#show").html(result);

            }
        });

    });

});

这是我的控制者:

$jqueryanimalid = $this->request->getdata('idString');

    $this->Cookie->write('animalid', $jqueryanimalid,(time()+3600*24)+(mktime(24,0,0)-time()));
    $cookie_animalid = $this->Cookie->read('animalid');

        $posts = $this->Posts->find('all', array(
            'conditions' => array(
                'Animal_id' => $cookie_animalid
            )
        ));
    $this->set(compact('posts'));

返回值是我想要的,如果我点击Try1,它会保存像名称:animalid和Value:1 这样的Cookie。最大的问题是,刷新所有已经消失的饼干和我想拥有的价值!为什么?有人能帮助我吗?

0 个答案:

没有答案