JS cookie有时候没有设置

时间:2017-01-13 08:06:21

标签: javascript cookies

如果我的头部前面我这样做:

<head>
  <script>
    function setCookie(cname, cvalue, exdays) {
      var d = new Date();
      d.setTime(d.getTime() + (exdays*24*60*60*1000));
      var expires = "expires="+ d.toUTCString();
      document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
    }

    setCookie('testCookie', 'testCookieValue', 1);

    function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
        (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
        m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
        })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

    ga('create', 'UA-XXXXXXX-X', 'auto');
    ga('send', 'pageview');
  </script>
  ..
</head>  

稍后用django(python框架)我检查cookie是否存在:

if 'testCookie' in request.COOKIES:
    testValue = request.COOKIES['testCookie']  
else:
    testValue = 'no Value'

有时testValue返回&#34; testCookieValue&#34;有时&#34;没有价值&#34;。什么原因导致cookie没有设置?

0 个答案:

没有答案