Javascript 如何使用 POST 方法和 JS submit() 传递表单数据?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8536842/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 06:24:53  来源:igfitidea点击:

How to pass form data using POST method and JS submit()?

javascripthtml

提问by ihtus

How to pass form data using POST method and JS submit() ?

如何使用 POST 方法和 JS submit() 传递表单数据?

I would like not to use AJAX.

我不想使用 AJAX。

I can achieve that by using JS and GET method, but my goal now is to use JS (probably with submit() ) and POST method.

我可以通过使用 JS 和 GET 方法来实现,但我现在的目标是使用 JS(可能使用 submit() )和 POST 方法。

Please give a simple working example.

请给出一个简单的工作示例。

Thank you!

谢谢!

Edited: Sorry, I will be more specific. Here is a simple code I have:

编辑:对不起,我会更具体。这是我有一个简单的代码:

<form name=\"myform\" action=\"\">
  Search: <input type='text' name='query' />
  <a href=\"javascript: submitform()\">Search</a>
</form>

 <script type=\"text/javascript\">
 function submitform() {   document.myform.submit(); }
 </script>

When I insert "word" into the input field and press Search, the URL becomes "../index.php?query=word" and that means that form data is passed like GET method.

当我在输入字段中插入“word”并按“搜索”时,URL 变为“../index.php?query=word”,这意味着表单数据像 GET 方法一样传递。

And my goal is to remove any form data from URL and pass it like with POST method.

我的目标是从 URL 中删除任何表单数据并像使用 POST 方法一样传递它。

Edited: ohh.. I just added method=post and no form data in the URL anymore :)

编辑:哦..我刚刚添加了 method=post 并且 URL 中没有表单数据了 :)

回答by Madara's Ghost

Just have a form and submit it.

只要有一个表格并提交它。

form = document.forms[0] //assuming only form.
form.submit();


EDIT: OP has clarified question

编辑:OP澄清了问题

Specify a methodattribute on your form.

method在表单上指定一个属性。

<form name="myform" action="" method="POST">

It will cause the form to be submitted as a POST.

它将导致表单作为 POST 提交。

回答by Fabrizio Calderan

Set the form's methodattribute to POST:

将表单的method属性设置为 POST:

<form method="post">

And then submit the form via JavaScript using <formelement>.submit()

然后使用 JavaScript 通过 JavaScript 提交表单 <formelement>.submit()

回答by chrisf

As mentioned already, you can use a form. I find it useful to have a function which creates a form on-the-fly and submits it - that way you don't need to clutter your markup with forms until they're needed by your JS.

如前所述,您可以使用表单。我发现拥有一个动态创建表单并提交它的函数很有用 - 这样你就不需要用表单来混淆你的标记,直到你的 JS 需要它们。

function PostObjectToUri(uri, obj) {
    "use strict";

    var json, form, input;

    json = JSON.stringify(obj);

    form = document.createElement("form");
    form.method = "post";
    form.action = uri;
    input = document.createElement("input");
    input.setAttribute("name", "json");
    input.setAttribute("value", json);
    form.appendChild(input);
    document.body.appendChild(form);
    form.submit();
};

Obviously that example JSON-serializes an object into a string and sets it on a single input, but you could adapt it to do whatever you need.

显然,该示例 JSON 将对象序列化为字符串并将其设置在单个输入上,但您可以对其进行调整以执行任何您需要的操作。

EDIT: I'm not actually sure whether you even need to append to the DOM before submitting - can anyone clarify?

编辑:我实际上不确定您是否甚至需要在提交之前附加到 DOM - 任何人都可以澄清吗?

回答by Sandy Marwal

jQuery AJAX post is best way to do it. See below example code.

jQuery AJAX 帖子是最好的方法。请参阅下面的示例代码。

e.preventDefault();

$.ajax({
   type: 'post',
   url: 'data.php',
   data: $('form').serialize(),
   success: function(response) {
      $('#DisplayDiv').html(response);
   }
});