How to call existing REST api from a HTML form? How to call existing REST api from a HTML form? mongodb mongodb

How to call existing REST api from a HTML form?


Typically When user would like to get data from the server. client need to send a request to the server and get a response. Usually programmer will bind a request function with an specific element and events.

In this case you need to bind a request function with form element. As you didn't mention which event you want to happen, so I couldn't tell exactly solution.

The following code is a simple code that call REST API when user type on a text input, and show the result below the input text

Note that replace "URL" with your API call.

<!DOCTYPE html><html><body>    <form>        Keyword:<br>        <input type="text" name="keyword" onkeyup="callREST()"><br>    </form>    <div id="response"></div>    <script>        function callREST() {            var xhttp = new XMLHttpRequest();            xhttp.onreadystatechange = function() {                if (this.readyState == 4 && this.status == 200) {                    document.getElementById("response").innerHTML = this.responseText;                }            };            xhttp.open("GET", "URL", true);            xhttp.send();        }    </script></body></html>