如何从 html 文件触发 HTTP POST?

how to fire a HTTP POST from a html file?

我需要创建一个测试 html 文件,以便能够测试我创建的 API,但我根本不是 html 编码员。

所以,我有以下内容:

POST /somepage.php HTTP/1.1
Host: example.com
Content-Type: application/x-www-form-urlencoded
Content-Length: 19

name=ruturajv&sex=m

我试过将该代码放在文件中,但浏览器无法启动它。

编写一个简单的 HTML 文件的最好和最简单的方法是什么,我只需将其加载到浏览器中,它就会自动触发上述 POST 方法?

感谢大家的帮助!

创建一个简单的表单,其中包含名为 namesex 的字段以及带有 Submit 按钮的目标 http(s)://example.com/somepage.php

填写ruturajvm.

单击此按钮将发送请求。

<form action = "http://example.com/somepage.php" method = "post">
   <input type = "text" name = "name" value = "ruturajv">
   <input type = "text" name = "sex" value = "m">
   <p><input type = "submit"></p>
</form>

最简单:使用

<form>

form tag on MDN : 这个例子应该符合你的要求。

如果您想要更复杂的东西,请查找 XMLHTTPRequest