简体   繁体   English

如何从引导表单检索用户输入到 JSON 发布到 API?

[英]How can I retrieve the user input from a bootstrap form to JSON post to an API?

 <form>
        <div class="form-group">
            <label for="text">Name:</label>
            <input type="text" class="form-control" id="name" placeholder="Enter name" name="name">
        </div>
        <div class="form-group">
            <label for="text">Age:</label>
            <input type="text" class="form-control" id="age" placeholder="Age" name="age">
        </div>
        <div class="form-group">
            <label for="text">City:</label>
            <input type="text" class="form-control" id="city" placeholder="Enter city" name="city">
        </div>
        <button onclick = "MyFunction()" id = "submitButton" type="submit" class="btn btn-default">Submit</button>
        <p id = "demo">

        </p>
        <script>

            function MyFunction() {
                var name = document.getElementById("name")
                var age = document.getElementById("age")
                var city = document.getElementById("city")
                document.getElementById("demo").innerHTML = name + " " + age + " " + city;
            }
        </script>
    </form>
</div>

it's a bootstrap form where they fill their name, address, age, and when they press submit it makes a JSON post request to a flask server that is running on localHost:5200这是一个引导表单,他们填写他们的姓名、地址、年龄,当他们按下提交时,它会向在 localHost:5200 上运行的 flask 服务器发出 JSON 发布请求

that is the end goal, right now I am trying to just show on the screen what the user entered in these forms to show up on screen so I know im grabbing the right things hence the myFunction method, but it is not working and returns an object and refreshes the page...这是最终目标,现在我试图在屏幕上显示用户在这些 forms 中输入的内容以显示在屏幕上,所以我知道我抓住了正确的东西,因此 myFunction 方法,但它不起作用并返回一个object 并刷新页面...

<form>
    <div class="form-group">
      <label for="text">Name:</label>
      <input type="text" class="form-control" id="name" placeholder="Enter name" name="name">
    </div>
    <div class="form-group">
      <label for="text">Age:</label>
      <input type="text" class="form-control" id="age" placeholder="Age" name="age">
    </div>
    <div class="form-group">
      <label for="text">City:</label>
      <input type="text" class="form-control" id="city" placeholder="Enter city" name="city">
    </div>
    <button onclick="MyFunction(event)" id="submitButton" type="submit" class="btn btn-default">Submit</button>
    <p id="demo">

    </p>
  </form>
  <script>
    function MyFunction(e) {
      e.preventDefault();
      var name = document.getElementById("name").value
      var age = document.getElementById("age").value
      var city = document.getElementById("city").value
      document.getElementById("demo").innerHTML = name + " " + age + " " + city;
      fetch('http://localhost:5100', {
        method: 'POST',
        body: JSON.stringify({ name, age, city })
      }).then(res => res.json())
    }
  </script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将用户输入从表单分配到外发文件 - How can I assign the user input from a form to a outfile 如何通过HTML表单输入文本并使用Javascript从JSON文件中检索数据作为响应? - How do I input text via an HTML form and retrieve data from a JSON file as a response using Javascript? 如何延迟来自用户表单的 POST 请求,直到收到来自 Stripe 的 webhook POST 之后? - How can I delay a POST request from a user form until AFTER a webhook POST from Stripe is received? 如何使用AJAX发布方法从PHP的特定函数中检索JSON - How can I retrieve JSON from PHP's specific function using AJAX post Method 如何在不使用 JQuery 的情况下使用 fetch API 以 JSON 格式发布表单数据? - How can I POST a form data in JSON format using fetch API without using JQuery? 从引导表单获取用户输入值 - Getting User Input Value from Bootstrap Form 如何在React Bootstrap上获取输入表单的值? - How can I grab the value of an input form on React Bootstrap? 用户输入如何以bootstrap 3形式提交? - How is user input submitted in bootstrap 3 form? 如何将表单中的数据发布到本地 JSON 文件? - How can I POST data from my form to a local JSON file? arangodb,如何从发布请求中检索JSON - arangodb, How to retrieve JSON from Post request
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM