I need to write and read a cookie and I'd like to use the jquery-cookie plugin.

However I cannot manage to store a cookie and read it back.

Here's a MWE

<html>
<head>
    <title>Test</title>
    <script type="text/javascript" src="jquery.js"></script>
    <script type="text/javascript" src="jquery-cookie/jquery.cookie.js"></script>
    <script type="text/javascript">
    $(function() {
        $.cookie('aCookie','aValue');
        console.log($.cookie('aCookie'));
    });
    </script>
</head>
<body>
</body>
</html>

I would expect to see aValue printed out in the console, whereas it prints null, i.e. the cookie has not been found.

I feel there is something obvious I'm missing here.

有帮助吗?

解决方案

I am going to guess that the url in your browser points to local file so it is file:///C:/some/path.html

Chrome does not allow saving of cookies on the local files. Cookies are strictly a HTTP mechanism (RFC 2109)

You can enable them with the command line flag --enable-file-cookies.

其他提示

Make sure you are not running the file from file://. Cookies are set by a server and I've had issues in the past if running off the file system

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top