使我的cookie在mvc中工作 [英] Making my cookies work in mvc
问题描述
我需要切换我的mvc主题,它的工作,但实现的cookie不工作。
I need to switch my mvc theme and it's working, but the implemented cookie doesn't work at all.
var style_cookie_name = "submitTheme";
var style_cookie_duration = 30;
var style_domain = "localhost";
function switch_style(css_title) {
var i, link_tag;
for (i = 0, link_tag = document.getElementsByTagName("link") ;
i < link_tag.length ; i++) {
if ((link_tag[i].rel.indexOf("stylesheet") != -1) &&
link_tag[i].title) {
link_tag[i].disabled = true;
if (link_tag[i].title == css_title) {
link_tag[i].disabled = false;
}
}
set_cookie(style_cookie_name, css_title,
style_cookie_duration, style_domain);
}
}
这是我在更改主题时触发的函数,除了它将值传递给set_cookie()函数的部分:
This is the function I'm triggering while changing themes, and except the part that it passes values to set_cookie() function:
function set_cookie(cookie_name, cookie_value,
lifespan_in_days, valid_domain) {
var domain_string = valid_domain ?
("; domain=" + valid_domain) : '';
document.cookie = cookie_name +
"=" + encodeURIComponent(cookie_value) +
"; max-age=" + 60 * 60 *
24 * lifespan_in_days +
"; path=/" + domain_string;
}
根据我有限的知识,这个脚本应该保存一个cookie域名,这不会发生(网址地址保持不变)。
According to my limited knowlefge, this script should save a cookie at the end of the domain, which doesn't happen (the url address remains the same).
你能帮助我吗?提前感谢!
Can you please assist me? Thanks in advance!
推荐答案
对于有其他问题的人:当您在localhost上运行您的网站时, 。这固定了我!
For everybody else having that problem: when you run your site on localhost, you should set your domain to "". This fixed it for me!
这篇关于使我的cookie在mvc中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!