【问题标题】:POST redirections using Javascript使用 Javascript 进行 POST 重定向
【发布时间】:2018-07-04 16:13:30
【问题描述】:

如果您想将您网站的用户重定向到某个 URL,您可能需要使用 window.location.replace('/resource') 函数。问题是它只对给定的 URL 进行 GET 请求。

如果你想使用另一个动词(比如 POST),你可能想使用这样的形式:

<form method="POST" action="/resource">
  <button type="submit">Submit</button>
</form>

但是又出现了一个问题,它需要用户点击提交按钮才能真正开始重定向。

我应该如何将我网站的用户重定向到 POST 路由而不需要他点击表单的提交按钮?

【问题讨论】:

    标签: html forms redirect post


    【解决方案1】:

    试试这个:

    function submit() {
      document.getElementById('myForm').submit();
    }
    <!-- Place this button anywhere you want in the body (it can even be in a form) -->
    <button type="button" onclick="submit()">Submit</button>
    
    <!-- Place this form outside of another form -->
    <form action="/resource" method="POST" id="myForm"></form>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-31
      • 1970-01-01
      • 1970-01-01
      • 2012-01-31
      • 1970-01-01
      • 2013-04-04
      相关资源
      最近更新 更多