简体   繁体   English

Chrome未将Cookie路径设置为root

[英]Chrome not setting cookie path to root

I am setting a cookie in Javascript using the following code : 我使用以下代码在Javascript中设置cookie:

setCookie('cart_items','product_name');


function setCookie(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

But the cookie path is not set to root (/) in Chrome. 但是Chrome路径中的cookie路径未设置为root(/)。 Instead it gets set to the path from where the web page is being executed !! 相反,它被设置为从执行网页的路径!!

I tested with IE and FF. 我用IE和FF测试过。 It works fine with both these browsers .... 它适用于这两种浏览器....

What might be wrong with Chrome or Is it the problem with cookie creation code i am using?? Chrome可能有什么问题,或者我正在使用的cookie创建代码存在问题?

In Chrome ( 16.0.912.63 ) 在Chrome中(16.0.912.63)

Path: /xxxxxxxx/xxxxxxx 路径:/ xxxxxxxx / xxxxxxx

in FF ( 6.0 ) 在FF(6.0)

Path: / 路径:/

in IE (9) 在IE中(9)

Path: / 路径:/

The reason this happens is because chrome doesn't allow setting cookies on local files by default. 发生这种情况的原因是因为chrome默认情况下不允许在本地文件上设置cookie。 See this answer for more information: https://stackoverflow.com/a/347997/1324019 (text from answer) 有关详细信息,请参阅此答案: https//stackoverflow.com/a/347997/1324019 (来自答案的文字)

Chrome doesn't support cookies for local files (or, like Peter Lyons mentioned, localhost*) unless you start it with the --enable-file-cookies flag. Chrome不支持本地文件的Cookie(或者像Peter Lyons所提到的,localhost *),除非您使用--enable-file-cookies标志启动它。 You can read a discussion about it at http://code.google.com/p/chromium/issues/detail?id=535 . 您可以在http://code.google.com/p/chromium/issues/detail?id=535上阅读有关该讨论的讨论。

*Chrome does support cookies if you use the local IP address (127.0.0.1) directly. *如果您直接使用本地IP地址(127.0.0.1),Chrome 支持Cookie。 so in the localhost case, that could be an easier workaround. 所以在localhost的情况下,这可能是一个更容易的解决方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM