在设置HTTP cookie时,我可以使用localhost作为域吗? [英] Can I use localhost as the domain when setting an HTTP cookie?

查看:2667
本文介绍了在设置HTTP cookie时,我可以使用localhost作为域吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用jQuery插件来设置cookie,当我使用localhost作为域时,它不会存储cookie。

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

这里是我使用的插件jQuery 1.2.6。

Here is the plugin I am using with jQuery 1.2.6.

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

下面是我使用的代码。你可以看到它不喜欢localhost,我从localhost上的开发web服务器运行它。一个细节是,我运行的端口4005,但不应影响域,AFAIK。

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' });

});


推荐答案

创建一个域名,并将其添加到您的主机文件中为127.0.0.1。然后在该域上运行Web应用程序。

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.

这篇关于在设置HTTP cookie时,我可以使用localhost作为域吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆