【问题标题】:Why does "?a=a" get added to the back of my URL on GET?为什么在 GET 上我的 URL 后面会添加“?a=a”?
【发布时间】:2021-11-27 16:37:49
【问题描述】:

我使用带有action="/sact/${searchp}/" 的post GET 请求来提交用户搜索查询。

var searchp = search.querySelector("input.search-input").value;
// returns the normal value

document.body.innerHTML += `<form id="jsForm" action="/sact/${search}/" method="GET"><input type="hidden" name="a" value="a"></form>`;
document.getElementById("jsForm").submit();

这里重定向:

router.get('/sact/:where', async (req, res, next) => {
  res.render('search');
});

我不明白为什么 /?a=a 每次提交时都会添加到 URL 的后面。如何删除它?

【问题讨论】:

  • 如果您在提交表单后不想在 URL 中包含任何查询字符串,请使用 POST 而不是 GET 方法

标签: javascript node.js forms url get


【解决方案1】:

当您document.getElementById("jsForm").submit(); 将在 URL 上发送表单作为查询参数时,因为您使用的是 method="GET"

a=a 来自该字段 &lt;input type="hidden" name="a" value="a"&gt; 隐藏在 HTML 中,但仍将通过 URL 发送

如果你想“隐藏”你可以使用method="POST" 将不会在 URL 上可见

【讨论】:

  • 啊,这很有意义。谢谢
猜你喜欢
  • 1970-01-01
  • 2014-02-19
  • 2018-06-10
  • 1970-01-01
  • 2023-02-20
  • 2012-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多