jquery cookie不工作 - 相同的域不同的文件夹 [英] jquery cookie not working - same domain different folder
问题描述
我在子目录中有一个html页面:
I have a html page in a sub directory:
http://mydomain.com/subDir/mag.html
该文件使用$ .cookie设置cookie,然后加载主索引页面,如下所示:
That file sets a cookie using $.cookie, then loads the main index page like so:
$.cookie("something",value);
alert( $.cookie("something") ); // test code: this shows the cookie correctly
window.open("../index.html","_self");
但是当我这样做的时候,我松开了cookie(显示为undefined)。如果我把mag.html放在与index.html相同的目录级别,它工作。
But when I do that, I loose the cookie (shows as undefined). If I put mag.html on the same directory level as index.html, it works.
我可以解决这个问题,但是想知道为什么会这样,
I can work around this but was wondering why this is so, since the domain hasn't changed.
推荐答案
一个路径没有访问另一个Cookie的权限,除非它是一个较低级别的路径
One path doesn't have access to another's cookies, unless it is a lower level path (that is, closer to the 'core').
您的cookie设置在 subDir
中。您必须将其设置为主索引文件的路径,以便能够访问此Cookie。
Your cookie is set in subDir
. You have to set it to the path of the main index file so to be able to access this cookie there.
为了设置自定义路径,您必须传递第三个参数到 $。cookie()
方法,这是一个配置对象。
In order to set a custom path, you will have to pass a third argument to the $.cookie()
method, which is a configuration object.
code> $。cookie(something,value,{path:'/'});
So: $.cookie("something", value, { path: '/' });
这篇关于jquery cookie不工作 - 相同的域不同的文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!