Cookie 为空,但在浏览器中有值
Cookie ist null, but has a value in the browser
我正在尝试设置一个存储当前 TimezoneOffset 的 cookie。
window.onload = function () {
var timezone_cookie = "timezoneoffset";
if (!$.cookie(timezone_cookie)) { // if the timezone cookie not exists create one.
// check if the browser supports cookie
var test_cookie = 'test cookie';
$.cookie(test_cookie, true);
if ($.cookie(test_cookie)) { // browser supports cookie
// delete the test cookie.
$.cookie(test_cookie, null);
// create a new cookie
$.cookie(timezone_cookie, new Date().getTimezoneOffset());
location.reload(); // re-load the page
}
}
else { // if the current timezone and the one stored in cookie are different then
// store the new timezone in the cookie and refresh the page.
var storedOffset = parseInt($.cookie(timezone_cookie));
var currentOffset = new Date().getTimezoneOffset();
if (storedOffset !== currentOffset) { // user may have changed the timezone
$.cookie(timezone_cookie, new Date().getTimezoneOffset());
location.reload();
}
}
};
但是当我稍后尝试读取该值时,Visual Studio 中的调试工具告诉我,它是 "null"。
如果我加载页面并检查 cookie,则会设置一个值 (-120)。
有人可以告诉我我做错了什么吗?
javascript 在 _Layout.cshtml 文件中。我想用 cookie 执行的代码如下所示:
var timeOffSet = HttpContext.Current.Session["timezoneoffset"]; // read the value from session
if (timeOffSet != null)
{
var offset = int.Parse(timeOffSet.ToString());
dt = dt.AddMinutes(-1 * offset);
return dt.ToString("yyyy-MM-dd HH:mm");
}
// if there is no offset in session return the datetime in server timezone
return dt.ToLocalTime().ToString("yyyy-MM-dd HH:mm");
函数是从View调用的,此时js代码应该已经执行完毕
我认为您尝试以错误的方式获取值 - cookie 未存储在会话变量中。
您可以改用这一行:
HttpCookie cookie = Request.Cookies["timezoneoffset"];
if ((cookie != null) && (cookie.Value != ""))
{
var offset = int.Parse(cookie.Value.ToString());
dt = dt.AddMinutes(-1 * offset);
return dt.ToString("yyyy-MM-dd HH:mm");
}
return dt.ToLocalTime().ToString("yyyy-MM-dd HH:mm");
我正在尝试设置一个存储当前 TimezoneOffset 的 cookie。
window.onload = function () {
var timezone_cookie = "timezoneoffset";
if (!$.cookie(timezone_cookie)) { // if the timezone cookie not exists create one.
// check if the browser supports cookie
var test_cookie = 'test cookie';
$.cookie(test_cookie, true);
if ($.cookie(test_cookie)) { // browser supports cookie
// delete the test cookie.
$.cookie(test_cookie, null);
// create a new cookie
$.cookie(timezone_cookie, new Date().getTimezoneOffset());
location.reload(); // re-load the page
}
}
else { // if the current timezone and the one stored in cookie are different then
// store the new timezone in the cookie and refresh the page.
var storedOffset = parseInt($.cookie(timezone_cookie));
var currentOffset = new Date().getTimezoneOffset();
if (storedOffset !== currentOffset) { // user may have changed the timezone
$.cookie(timezone_cookie, new Date().getTimezoneOffset());
location.reload();
}
}
};
但是当我稍后尝试读取该值时,Visual Studio 中的调试工具告诉我,它是 "null"。
如果我加载页面并检查 cookie,则会设置一个值 (-120)。 有人可以告诉我我做错了什么吗?
javascript 在 _Layout.cshtml 文件中。我想用 cookie 执行的代码如下所示:
var timeOffSet = HttpContext.Current.Session["timezoneoffset"]; // read the value from session
if (timeOffSet != null)
{
var offset = int.Parse(timeOffSet.ToString());
dt = dt.AddMinutes(-1 * offset);
return dt.ToString("yyyy-MM-dd HH:mm");
}
// if there is no offset in session return the datetime in server timezone
return dt.ToLocalTime().ToString("yyyy-MM-dd HH:mm");
函数是从View调用的,此时js代码应该已经执行完毕
我认为您尝试以错误的方式获取值 - cookie 未存储在会话变量中。
您可以改用这一行:
HttpCookie cookie = Request.Cookies["timezoneoffset"];
if ((cookie != null) && (cookie.Value != ""))
{
var offset = int.Parse(cookie.Value.ToString());
dt = dt.AddMinutes(-1 * offset);
return dt.ToString("yyyy-MM-dd HH:mm");
}
return dt.ToLocalTime().ToString("yyyy-MM-dd HH:mm");