【问题标题】:Replacing GET with POST用 POST 替换 GET
【发布时间】:2013-11-14 04:36:04
【问题描述】:

我在这里尝试重定向新页面,我想发送一个变量。它使用 GET。

我怎样才能用 POST 做到这一点?

window.location.href = 'start.php?userid='+userid;

start.php

<?php
    $user_id=$_GET['userid']; //should be post
?>

【问题讨论】:

  • 为什么需要使用POST
  • 另外注意手动传递变量名和值时需要使用encodeURIComponent
  • @moonwave99:很容易被黑

标签: javascript php ajax


【解决方案1】:

您可以通过 html 标签和 .否则,请参阅 "jQuery.post" 了解异步帖子。
对于您创建 FORM 标记的第一种情况,将 INPUT type="hidden" 放入其中并设置其将被发布的值。

【讨论】:

  • 请仅使用 ajax 调用。我无法使用 jquery
  • @Karimkhan w3schools.com/ajax/ajax_xmlhttprequest_send.asp 请参阅“发布请求”部分
  • 不推荐 w3schools.com 作为参考。它充满了错误,请参阅w3fools.com
  • 除非要求 jQuery 答案,或者它在标签中,否则请不要使用 jQuery 回答问题。这不是一个标准。
【解决方案2】:

您需要在 HTML 中声明一个伪造的表单和一个链接,该链接将触发 javascript 提交表单,如下所示

<form action="start.php" method="post">
<input type="hidden" name="userid" value="[userid]">
</form>

<script type="text/javascript">
function submitlink() 
{
document.forms[0].submit();
}
</script>
<a class="buttondown" onclick="submitlink()">Submit Link</a>

【讨论】:

  • 如何获取我发布的 var 以及如何在另一端接收它/?
【解决方案3】:

您必须将数据提交到服务器,而不仅仅是重定向浏览器。

在您的情况下,看起来您无论如何都希望刷新整个页面,只需动态创建一个表单:

var oForm = document.createElement("form");
oForm.method = "POST";
oForm.action = "start.php";
var oInput = document.createElement("input");
oInput.name = "userid";
oInput.value = userid;
oForm.appendChild(oInput);
document.body.appendChild(oForm);
oForm.submit();

【讨论】:

    【解决方案4】:

    我认为这个答案可能会对您有所帮助:
    How do you force a web browser to use POST when getting a url?

    您只需要使用 javascript 按需创建一个表单,以便在单击链接时使用 POST 方法发送数据。
    所以基本上就是这部分:

    <script type="text/javascript">
    function submitAsPost(url) {
        var postForm = document.createElement('form');
        postForm.action = url;
        postForm.method = 'post';
        var bodyTag = document.getElementsByTagName('body')[0];
        bodyTag.appendChild(postForm);
        postForm.submit();
    }
    </script>
    <a href="/my/url" onclick="submitAsPost(this.href); return false;">this is my post link</a>
    

    【讨论】:

      【解决方案5】:

      我遇到了同样的问题。如果你使用 jquery.redirect.min.js 作为你的 jquery 插件,你可以使用如下。它为您提供 POST 方法。

         $().redirect("myPhp.php", { name: "John" });
      

      您只需要here 下载 jquery.redirect.min.js 文件,然后将它与您的 php 文件链接 em>,如上使用。而已。希望我能帮上忙。

      对我来说很好。

      【讨论】:

      • 默认情况下是否使用'POST'方法进行重定向?
      • 是的。这个默认方法是“POST”。
      • 啊哈。通常,插入与另一个答案的建议相同,但方式简单。所以,+1。
      猜你喜欢
      • 1970-01-01
      • 2011-09-25
      • 2022-10-20
      • 1970-01-01
      • 2018-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-20
      相关资源
      最近更新 更多