Html POST 表单的内容以休息 api

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19932801/
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-29 15:15:52  来源:igfitidea点击:

POST contents of a form to rest api

javascripthtmlhttprest

提问by bookthief

I have made a html user login form:

我制作了一个 html 用户登录表单:

<html >
<head>

<title>User Login</title>
<link rel="stylesheet" href="css/bootstrap.css">
<link rel="stylesheet" href="css/signin.css">
</head>

<body>
<div class="container">
<form class="form-signin">
<h2 class="form-signin-heading">Please sign in</h2>
<input class="form-control" type="text" autofocus="" required="" placeholder="Email address">
<input class="form-control" type="password" required="" placeholder="Password">
<label class="checkbox">

</label>
<a href= "" <button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button></a>
</form>
</div>
</body>
</html>

I now need to send the contents of the form, ie the username and password, to a rest api for authentication using the http POST command. I have no idea where to start with this and am having trouble finding a tutorial which assumes absolutely no knowledge of rest APIs and http commands. I'm extremely new to web development, can anyone point me in the direction of a good tutorial, or show me an example of what the javascript (I presume) might look like?

我现在需要使用 http POST 命令将表单的内容(即用户名和密码)发送到 rest api 以进行身份​​验证。我不知道从哪里开始,并且无法找到一个教程,该教程假设完全不了解 rest API 和 http 命令。我对 Web 开发非常陌生,任何人都可以向我指出一个好的教程的方向,或者向我展示 javascript(我认为)可能是什么样子的示例?

采纳答案by carter

First fix your HTML. Then with the following no javascript is necessary:

首先修复您的 HTML。然后使用以下不需要 javascript:

<form class="form-signin" method="POST" action="URL_OF_REST">
  <h2 class="form-signin-heading">Please sign in</h2>
  <input class="form-control" type="text" required name="email" placeholder="Email address">
  <input class="form-control" type="password" required name="password" placeholder="Password">
  <label class="checkbox"></label>
  <button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
</form>