2013-04-11 191 views
0

我想用jQuery设置一个cookie,但它根本没有显示cookie。 有人可以解释我在哪里做错了吗? http://jsfiddle.net/RVFX4/1/jQuery cookie设置问题

我有这个成立于我的index.html:

$(document).ready(function() { 

     $.cookie("test", 1, { 
      expires : 10,   

      path : '/',   

      domain : 'jquery.com', 


      secure : true 

     }); 

    }); 

我应该得到一个名为test的cookie的1正确的值?

+0

您使用的选项有问题。不知道是什么,但如果除去'expires'之外的所有东西,那么它就可以工作。 – Archer 2013-04-11 11:41:31

回答

0

如果你读的小提琴评论:

path : '/',   //The value of the path attribute of the cookie 
         //(default: path of page that created the cookie). 

domain : 'jquery.com', //The value of the domain attribute of the cookie 
         //(default: domain of page that created the cookie). 

secure : true   //If set to true the secure attribute of the cookie 
         //will be set and the cookie transmission will 
         //require a secure protocol (defaults to false). 

显然的域名并没有jquery.com,路径不是根或者(因为实际的代码页中的子域托管)。最后,协议是纯http,因此secure: true要求可以防止创建cookie。

如果你删除这些行它在jsfiddle中工作,并且它应该工作,如果参数在其他域中正确给定。除非有特定的理由,否则你根本不需要给他们。

+0

即使只在我的本地使用“expires:10”,它仍然不会设置cookie,但它可以在您的建议下继续工作... – Alex 2013-04-11 11:53:21

+0

您是否在计算机上设置了服务器?你不能在本地文件系统中设置cookie('file:///'url)。 – JJJ 2013-04-11 12:15:00