【发布时间】: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