Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
304 views
in Technique[技术] by (71.8m points)

javascript - Can I use localhost as the domain when setting an HTTP cookie?

I am using a jQuery plugin to set cookies and when I use localhost for the domain it will not store the cookie.

Here is the plugin I am using with jQuery 1.2.6.

http://www.stilbuero.de/2006/09/17/cookie-plugin-for-jquery/

Below is the code that I am using. You can see it does not like localhost, and I am running it from a development web server on localhost. One detail is that I am running off port 4005 but that should not affect the domain, 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' });

});
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I had similar problem with setting cookies. Make up a domain name and add it to your hosts file as 127.0.0.1. Then run web application on that domain.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...