我正在使用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