Quantcast
Channel: Active questions tagged html - Stack Overflow
Viewing all articles
Browse latest Browse all 74020

how to Invoke api using simple html code?

$
0
0

I have a small requirement that needs to make REST API POST call using HTML code. I have copied the sample code below and some reason this is not working. I am unable to see any call using the below code. Please let me know If there is any change required in the code.

<html>

  <head>

  <title>My first API script</title>

  <link rel="stylesheet" type="text/css" href="style.css">
  </head>

  <body>

    <H1>Testing the API call</h1>

    <form>
      <label for="msg">Message</label>

      <button id='submitButton'>Submit</button>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
    <script type="text/javascript">
        var API_URL = 'https://example.com/'; // this will be my API like https://example.com

        $('#submitButton').on('click', function(){
          $.ajax({
            type: 'POST',
            url: API_URL,
            data: JSON.stringfly({"message": $('#msg'.val())}),
            contentType: "application/json",

            success: function(data){
              location.reload();
            }
          })
        })

    </script>

  </body>

</html>

Viewing all articles
Browse latest Browse all 74020

Trending Articles