【发布时间】:2023-04-08 23:07:01
【问题描述】:
我有来自http://fromDomain.com 的类似html 并发布到http://toDomain.com 这是一个rails 应用程序。
<html>
<h1>Test Redirect with Post</h1>
<button id="submit" onclick="send_form()">Submit</button>
<script>
function send_form() {
var form = document.createElement("form");
form.setAttribute("method", 'post');
form.setAttribute("action", 'http://todomain.com/someaction');
form.appendChild(addElement('field1', 'field1'));
form.appendChild(addElement('field2', 'field2)');
document.body.appendChild(form);
form.submit();}
function addElement(key, value){
var hiddenField = document.createElement("input");
hiddenField.setAttribute("type", "hidden");
hiddenField.setAttribute("name", key);
hiddenField.setAttribute("value", value);
return hiddenField;}
</script>
</html>
在 Rails 应用程序中,我正在做这样的事情。
def someaction
if not session[:token]
session[:token] = @self.getsomehash
end
end
问题是在http://todomain.com/someaction 页面加载之后,如果用户点击浏览器刷新,那么它将重新发布页面并且会话哈希被重置。看起来我在浏览器重新发布 rails 时会创建一个新会话而不是使用现有会话。
我的假设对吗?有没有办法允许刷新并仍然保留这些值?
【问题讨论】:
-
我对 RoR 托管环境不太熟悉,但是会话 cookie(瞬态 cookie)通常存储在用户代理中,以在这些情况下识别访问者。这种机制应该与 RoR 如何在服务器端进行状态管理无关。你用的是什么浏览器?
-
您能否使用浏览器的开发者模式来确定您的浏览器在首次发布后首次访问页面/站点时正在为 todomain.com> 存储会话 cookie?...它应该具有与会话 cookie 关联的域(范围)值,如果正在创建第一个会话 cookie,是否将 todomain.com> 指定为域?。
-
我正在使用 Chrome,如果您推荐,我可以使用任何浏览器进行测试。我还将检查域范围。
-
Chrome 很好,使用开发者控制台 [f12] > 资源.. 找到并删除 todomain>.. 然后运行你的帖子,刷新测试.. 看看会话 cookie 是否以及何时创建的 .. 会话 cookie 的过期日期为“
”
标签: javascript ruby-on-rails session