为什么 Chrome 会忽略本地 jQuery cookie? [英] Why does Chrome ignore local jQuery cookies?
问题描述
我正在使用 jQuery Cookie 插件(download 和 演示 和 带有注释的源代码) 来设置和读取 cookie.我正在本地机器上开发页面.
I am using the jQuery Cookie plugin (download and demo and source code with comments) to set and read a cookie. I'm developing the page on my local machine.
以下代码将在 FireFox 3、IE 7 和 Safari (PC) 中成功设置 cookie.但是如果浏览器是谷歌浏览器并且页面是本地文件,则它不起作用.
The following code will successfully set a cookie in FireFox 3, IE 7, and Safari (PC). But if the browser is Google Chrome AND the page is a local file, it does not work.
$.cookie("nameofcookie", cookievalue, {path: "/", expires: 30});
我所知道的:
- 该插件的演示适用于 Chrome.
- 如果我将代码放在网络服务器上(地址以 http://开头),它就可以在 Chrome 中使用.
- The plugin's demo works with Chrome.
- If I put my code on a web server (address starting with http://), it works with Chrome.
因此 cookie 仅在本地文件上的 Google Chrome 浏览器失败.
So the cookie fails only for Google Chrome on local files.
可能的原因:
- Google Chrome 不接受来自硬盘驱动器上网页的 cookie(路径如 file:///C:/websites/foo.html)
- 插件实现中的某些内容导致 Chrome 拒绝此类 cookie
谁能确认这一点并找出根本原因?
Can anyone confirm this and identify the root cause?
推荐答案
Chrome 不支持本地文件的 cookie(或者像 Peter Lyons 提到的 localhost*),除非你用 --enable-file-cookies 启动它旗帜.您可以在 http://code.google.com 上阅读有关它的讨论/p/chromium/issues/detail?id=535.
Chrome doesn't support cookies for local files (or, like Peter Lyons mentioned, localhost*) unless you start it with the --enable-file-cookies flag. You can read a discussion about it at http://code.google.com/p/chromium/issues/detail?id=535.
*Chrome 确实支持 cookie.所以在本地主机的情况下,这可能是一个更简单的解决方法.
*Chrome does support cookies if you use the local IP address (127.0.0.1) directly. so in the localhost case, that could be an easier workaround.
这篇关于为什么 Chrome 会忽略本地 jQuery cookie?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!