如何将表单数据发送到 link?

How do I send form data to a link?

我有一个带有表单的 HTML 页面,其中有 2 个输入、一个电子邮件和一个用户名。我想使用 fetch() 将这些输入传递给名为 exampleserver.com 的 node.js 快速服务器。服务器没有问题,但是表单有问题。这是我的 html 表格:

    <h1 align="center">Form</h1>
    <input type="text" placeholder ="person@email.com" id="email">
    <br>
    <input type="text" id="username" placeholder="person">
    <br>
    <button type="submit" onclick="myfunc()" id="demo">Click Me</button>

    <script type="text/javascript">
    var email = document.getElementById('email')
    var formattedemail = email.value.replace(/\./, '-') //periods dont work as a path in the express server, so this changes the period to a hyphen.
    var username = document.getElementById('username');
    function myfunc() {
    let fetchRes = fetch("https://exampleserver.com/inputform/" + formattedemail + "/" + username.value);
        fetchRes.then(res =>
            res.json()).then(response => {
                console.log(response.status)
            })
    }
    </script> 

谢谢!

您的代码在 jsonplaceholder api 上运行良好,请确保您的 API 按预期运行。

var email = document.getElementById('email')
var formattedemail = email.value.replace(/\./, '-') //periods dont work as a path in the express server, so this changes the period to a hyphen.
var username = document.getElementById('username');

function myfunc() {
  // temp data
  formattedemail = "todos";
  username.value = "1";
  
  let fetchRes = fetch("https://jsonplaceholder.typicode.com/" + formattedemail + "/" + username.value);
  fetchRes.then(res =>
    res.json()).then(response => {
    console.log(response);
  })
}
<h1 align="center">Form</h1>
<input type="text" placeholder="person@email.com" id="email">
<br>
<input type="text" id="username" placeholder="person">
<br>
<button type="submit" onclick="myfunc()" id="demo">Click Me</button>