Need example of calling AWS Lambda from JavaScript -


just need example of how call aws lambda javascript running in browser , display function result in javascript console. incredibly, cannot find examples on google or aws documentation.

my use case have html form. when form submitted, want use lambda process form inputs. assuming lambda function finishes no errors, want take user thank page.

please include complete html example, not code snippet.

i see people have used aws sdk javascript not required specially since need create amazon cognito identity pool access enabled unauthenticated identities (atleast beginners me). below code works fine me -

<html>     <head> <script>     function callawslambdafunction() {         var xhttp = new xmlhttprequest();         xhttp.onreadystatechange = function() {             if (this.readystate == 4 && this.status == 200) {                 document.getelementbyid("mydiv").innerhtml = this.responsetext;             }         };         xhttp.open("get", "https://test123.ap-south-1.amazonaws.com/dev", true);         xhttp.send();      }     </script>            <title>hello world!</title>     </head>     <body>         <h1>hello world!</h1>         <h1>click below button call api gatway , display result below!</h1>         <h1><div id="mydiv"></div></h1>         <button onclick="callawslambdafunction()">click me!</button><br>         regards,<br/>         aniket     </body> </html> 

above sample index.html have added s3 bucket , made static site. couple of points note -

  1. make index.html open outside if using s3 static site hosting.
  2. make sure turn on cors api gateway if website domain not same api gateway domain. else might -

cross-origin request blocked: same origin policy disallows reading remote resource @ https://test123.ap-south-1.amazonaws.com/dev. (reason: cors header ‘access-control-allow-origin’ missing).

source


Comments

Popular posts from this blog

dns - How To Use Custom Nameserver On Free Cloudflare? -

python - Pygame screen.blit not working -

c# - Web API response xml language -