最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to call existing REST api from a HTML form? - Stack Overflow

programmeradmin4浏览0评论

I have a REST API that I am using in a mobile application to register/store data into a Mongo database. I would now like to view the data stored in the DB on a webpage.

I know that I basically have all of the functionality already (the login request used in my mobile application) but I am so confused on how to call my REST from my HTML page.

Something like this: How to call a REST web service API from Javascript button Handler? ?

I am also confused on how/where I should be creating my html page. Any help is appreciated.

Thanks, Joe

I have a REST API that I am using in a mobile application to register/store data into a Mongo database. I would now like to view the data stored in the DB on a webpage.

I know that I basically have all of the functionality already (the login request used in my mobile application) but I am so confused on how to call my REST from my HTML page.

Something like this: How to call a REST web service API from Javascript button Handler? ?

I am also confused on how/where I should be creating my html page. Any help is appreciated.

Thanks, Joe

Share Improve this question asked Mar 3, 2018 at 15:42 BlueBullBlueBull 851 gold badge1 silver badge6 bronze badges 2
  • Using JS on the client side? – Luca Kiebel Commented Mar 3, 2018 at 15:44
  • Yes, node.js. Just a simple login webpage and then show users in a table is what I'm attempting – BlueBull Commented Mar 3, 2018 at 16:42
Add a ment  | 

2 Answers 2

Reset to default 8

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>

This is how you do it...

HTML Code:

<form id="form" onsubmit="return setAction(this)">                                       
        <input name="senderEmail" type="email" placeholder="Email" required>                                       
        <textarea  name="senderMessage" cols="30" rows="10" placeholder="Message.." required></textarea>
        <button type="submit">Send message</button>
</form>

Javascript:

function setAction(form) {
  const url = 'https://example./xyz?';
  const andSign = '&';
  const senderParameter = 'sender='+form.senderEmail.value;      
  const bodyParameter = 'body='+form.senderMessage.value;
  const newUrl = url+senderParameter+andSign+bodyParameter;

  fetch(
    newUrl,
    {            
        headers: { "Content-Type": "application/json" },            
        method: "POST",
        body: ""
    }
   )
  .then(data => data.json())
  .then((json) => {
    alert(JSON.stringify(json));
    document.getElementById("form").reset();     
  });
  return false;
}

This also resets your form after a successful api call is made.

发布评论

评论列表(0)

  1. 暂无评论