我正在使用jQuery插件来设置cookie,并且当我使用localhost作为域时,它不会存储cookie。设置HTTP cookie时,我可以使用localhost作为域吗?
这里是我使用jQuery 1.2.6的插件。
http://www.stilbuero.de/2006/09/17/cookie-plugin-for-jquery/
下面是我使用的代码。你可以看到它不像localhost,我从localhost上的开发Web服务器运行它。一个细节是我运行的端口4005,但不应该影响域,AFAIK。
$(function() {
console.log('Testing');
var one = $.cookie('Test.One');
var two = $.cookie('Test.Two');
var three = $.cookie('Test.Three');
console.log(['one', one]);
console.log(['two', two]);
console.log(['three', three]);
$('#div1').text(one);
$('#div2').text(two);
$('#div3').text(three);
$.cookie('Test.One', 'Test 1');
$.cookie('Test.Two', 'Test 2', { path: '/' });
$.cookie('Test.Three', 'Test 3', { path: '/', domain: 'localhost' });
});
是的,我应该阅读这篇文章,而不是丢失2个小时;-)“localhost”肯定是一个不好的价值! – 2012-08-13 13:03:36
只需使用“lvh.me”即可。此域名已指向127.0.0.1 – 2016-12-12 02:15:50