Javascript 为什么我的饼干会随机变化?
导言 我正在使用Rails 4开发一个应用程序,它可以作为一个简单的仪表板,它在主页上以象限分割的几个div容器中进行迭代,显示各种图形/表格等。我使用cookies来记住当前显示的容器所在的索引,因此当网站重新加载时,它会显示正在显示的容器,而不会返回到第一个容器 问题 一切似乎都正常,直到我开始注意到饼干开始自行改变,我不知道如何改变。我之所以得出这个结论,是因为我使用一个函数设置并获取cookies,每当调用它时,该函数都会打印到控制台。当这些“恶意”更改发生时,没有任何内容被记录到控制台 下面是它工作原理的要点。update函数使用一个helper函数来获取cookie,根据当前索引的内容计算下一个索引,然后调用另一个helper函数来存储cookie。cookies仅使用这两个函数获取和设置Javascript 为什么我的饼干会随机变化?,javascript,jquery,ruby-on-rails,cookies,Javascript,Jquery,Ruby On Rails,Cookies,导言 我正在使用Rails 4开发一个应用程序,它可以作为一个简单的仪表板,它在主页上以象限分割的几个div容器中进行迭代,显示各种图形/表格等。我使用cookies来记住当前显示的容器所在的索引,因此当网站重新加载时,它会显示正在显示的容器,而不会返回到第一个容器 问题 一切似乎都正常,直到我开始注意到饼干开始自行改变,我不知道如何改变。我之所以得出这个结论,是因为我使用一个函数设置并获取cookies,每当调用它时,该函数都会打印到控制台。当这些“恶意”更改发生时,没有任何内容被记录到控制台
var update_quadrant = function(id) =
$.when(get_cookie(current_feed)).done(function(response) {
var current_index = parseInt(response);
var next_index = //depends on current_index
$.when(store_cookie(current_feed, next_index, "from update")).done(function(response){
//code that updates quadrant
setTimeout(function() { update_quadrant(id);}, 30000);
});
下面是一些典型的饼干的样子
{"circleci" => "0", "newrelic" => "1", "googleanalytics" => "0"}
Javascript
存储cookie值
var store_cookie = function(feed, value, other) {
return $.ajax({
url: 'home/cookie_store',
type: 'POST',
data: {
feed: feed,
value: value,
other: other || ""
}
}).done(function(response) {
console.log(response);
});
};
#home controller
def store_cookie
feed = params[:feed]
value = params[:value]
other = params[:other]
cookies[feed] = value.to_s
render text: "stored #{value} for #{feed}, other: #{other}"
end
拿饼干
var get_cookie = function(feed) {
return $.ajax({
url: 'home/cookie_get',
data: {
feed: feed
}
}).done(function(response) {
console.log("Got cookie value: ", parseInt(response), "for ", feed);
});
};
控制器
响应ajax调用以存储cookie值
var store_cookie = function(feed, value, other) {
return $.ajax({
url: 'home/cookie_store',
type: 'POST',
data: {
feed: feed,
value: value,
other: other || ""
}
}).done(function(response) {
console.log(response);
});
};
#home controller
def store_cookie
feed = params[:feed]
value = params[:value]
other = params[:other]
cookies[feed] = value.to_s
render text: "stored #{value} for #{feed}, other: #{other}"
end
响应ajax对cookie值的调用
#home controller
def give_cookie
feed = params[:feed]
cookies[feed] = "0" if cookies[feed].blank?
render text: cookies[feed.to_s]
end
我尝试测试的内容
我认为setTimeout函数可能以某种方式持续存在,并且正在执行多个setTimeout计时器,但这并没有改变这样一个事实,即值正在更改,而没有从我的store_cookie函数将任何内容记录到控制台
例如,我设置了一个setInterval
,它每秒将document.cookie
记录到控制台。下面是将要显示的内容
"Stored 0 for circleci, other: from update quadrant"
//correctly changed with store_cookie function as it logs the response from the controller
{"circleci" => "0", "newrelic" => "1", "googleanalytics" => "0"}
...//no change
...//no change
...//no change
{"circleci" => "1", "newrelic" => "1", "googleanalytics" => "0"}
//but nothing printed to console from store_cookie function
//quadrant eventually goes to update after 30 seconds, and gets wrong cookie value
"Got cookie value: 1 for circleci" //should be 0 not 1
更令人困惑的是,当我点击一个使用不包含任何javascript源文件的布局的链接时,cookie值仍在更改
我一直在努力想从何处开始调试,因为我已经进行了两次和三次检查,以确保代码端的所有cookie更改都使用我的store_cookie函数进行了更改,并且我看到这些cookie值在没有任何记录到控制台的情况下被更改,aka,不调用store_cookie函数
有人知道是什么导致我的cookie值自行改变了吗?使用开发者工具的“网络”选项卡。单击您正在访问的URL,进入“Cookies”子选项卡,查看正在返回的响应Cookies。@Barmar好的,我会尽快仔细查看这些响应Cookies,谢谢您的建议。请使用“开发人员工具”的“网络”选项卡。单击您正在访问的URL,进入“Cookies”子选项卡,查看正在返回的响应Cookies。@Barmar好的,我会尽快仔细查看这些Cookies,谢谢您的建议。