【问题标题】:Javascript Not Setting CookiesJavascript 未设置 Cookie
【发布时间】:2020-07-27 05:20:10
【问题描述】:

我正在尝试使用 Javascript 设置 cookie。这段代码一直在工作,直到最近我开始收到 null 的 cookie 值。我真的不确定我可以改变什么来导致这种情况。这是我的HTML:

  <div class="w3-container w3-center" style="align-content:center;">
      <br>
      <br>
      <h1 class="w3-center w3-jumbo">Find a Fruit Here</h1>
      <form method="post" name="input-form" id="input-form" class="w3-center w3-mobile">
        <h4 class="w3-center" style="max-width:1000px;align-content:center;">
          <label>Address</label>
        </h4>
        <input id="input-address" style="max-width:300px;margin-left:auto;margin-right:auto" class="w3-center w3-input w3-border" name="Address" placeholder="123 45th Street, City, State" required>          
      
        <h4 class="w3-center" style="max-width:1000px;">
          <label>Fruits(at least one)</label>
        </h4>
        <input id="input-items" style="max-width:300px;margin-left:auto;margin-right:auto" class="w3-center w3-input w3-border" name="Items" placeholder="Apples, bananas, etc." required>
        <h4>
          <button style="max-width:250px;margin-left:auto;margin-right:auto" class="w3-button w3-black w3-round w3-hover-light-grey w3-center" type="submit" onclick="cookies()">
            Search
          </button>
        </h4>
      </form>      
      <br>
  </div>

这是我的 Javascript。我添加了一条日志记录行进行测试,但从未打印到控制台,所以我想我可能在调用该函数时遇到问题。

<script>

function cookies() {
  document.getElementById("input-form").submit();
  document.cookie = "address=" + document.getElementById("input-address").value.replaceAll(",", "/");
  document.cookie = "items=" + document.getElementById("input-items").value.replaceAll(",", "/");
 }

</script>

非常感谢任何帮助!我很茫然。

编辑: 我正在使用 Javalin 在 Java 程序中访问我的 cookie,这就是我看到它们为空的地方。我在 Javalin 中呈现文档,然后尝试访问 cookie,然后得到 NullPointerException。这是Java代码:

app.get("/search", ctx -> ctx.render("/donation-find.html"));
app.post("/search", ctx -> {
   ctx.render("/donation-find.html");
   address = ctx.cookie("address").replaceAll("/", ",");
   String[] temp = ctx.cookie("items").split("/");
}       

【问题讨论】:

  • 能否详细说明上述 HTML 和 JavaScript 是如何与 Javalin 交互的?这是否在与 Java Web 服务器通信的浏览器中运行,并且 Javalin 是 Web 服务器的一部分?

标签: javascript html cookies javalin


【解决方案1】:

您在更新 Javascript 上的 cookie 之前提交表单。 所以实际的 POST 请求是在 cookie 更新之前发送的
我认为你应该这样做

function cookies() {
  document.cookie = "address=" + document.getElementById("input-address").value.replaceAll(",", "/");
  document.cookie = "items=" + document.getElementById("input-items").value.replaceAll(",", "/");
 }
document.getElementById("input-form").submit();

我认为这会有所帮助
我不知道它之前是如何工作的
最好的猜测是它正在提取旧的会话 cookie

【讨论】:

  • 我赞成你的回答,因为它确实有帮助,但我能够以不同的方式解决我的问题
  • 谢谢,您能说明一下您是如何解决问题的吗??
  • 是的,我刚刚发布了一个新答案。
【解决方案2】:

为了澄清起见,我最终通过使用完全不同的数据传输方法解决了这个问题。我没有使用 cookie,而是使用 Javalin 内置方法 formParam(key) 来访问发布到表单的数据

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-03
    • 1970-01-01
    • 2012-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-10
    相关资源
    最近更新 更多