Why are all my jQuery cookies turning up undefined?

北战南征 提交于 2019-12-07 17:52:14

问题


I've sourced jQuery, the jQuery Cookie Plugin, and my own Javascript (in that order) at the end of the body of my page. What I'm unable to grasp is why my cookie won't read when I console.log the cookie, or when I assign it to a variable and console.log the variable. Also, when I open the page in Chrome and look through the Resources>Cookies section, it shows that my page has created no cookies.

Also, the problem may have to do with my operating on the file locally. It isn't hosted yet. Everything is just sitting on my drive, not in the internet yet.

Here's my code:

$.cookie('testcookie',1,{expires: 5, path:'/'});
console.log($.cookie('testcookie'));
var variable=$.cookie('testcookie');
console.log(variable);

That function should log '1' in the console twice, but just gives me two instances of undefined.

I've checked my HTML four times now, and I'm 100% sure the sources for my scripts are pointed to the correct place. I've also made sure the function is being called correctly, etc. I've checked all the probable problems, but I just can't find anything wrong. Am I missing something?


回答1:


Yeah, it sounds like it is because it's hosted locally.

See: Why does Chrome ignore local jQuery cookies?



来源:https://stackoverflow.com/questions/14946625/why-are-all-my-jquery-cookies-turning-up-undefined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!