2021年1月3日星期日

Any ideas on API gateway URL error (SES, API Gateway, Lambda integration with javascript/html)?

I've created websites with php web forms before, but I'm launching out to try to use a Lambda / API Gateway / SES combination in AWS while launching a website from S3, in order to create a dynamic submission form. If you'd like to take a quick look at the submission form (and error), it's here: https://precious-gemstones.com/about.html

Here is my javascript, which I've stored at the root level in my S3 bucket:

function submitToAPI(e) {         e.preventDefault();         var URL = "https://a78agcql46.execute-api.us-east-1.amazonaws.com/email";                var Namere = /[A-Za-z]{1}[A-Za-z]/;              if (!Namere.test($("#name-input").val())) {                           alert ("Name must be at least 2 characters.");                  return;              }                if ($("#email-input").val()=="") {                  alert ("Please enter your email.");                  return;              }                var reemail = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,6})?$/;              if (!reemail.test($("#email-input").val())) {                  alert ("Please enter a valid email address.");                  return;              }           var name = $("#name-input").val();         var email = $("#email-input").val();         var message = $("#message-input").val();         var data = {            name : name,            email : email,            message : message          };           $.ajax({           type: "POST",           url : "https://a78agcql46.execute-api.us-east-1.amazonaws.com/email",           dataType: "json",           crossDomain: "true",           contentType: "application/json; charset=utf-8",           data: JSON.stringify(data),                        success: function () {             // clear form and show a success message             alert("Thank you for your inquiry!");             document.getElementById("contact-form").reset();         location.reload();           },           error: function () {             // show an error message             alert("Error; please try again.");           }});       }  

Here is my Lambda function:

var AWS = require('aws-sdk');  var ses = new AWS.SES();     var RECEIVER = 'inquiry@precious-gemstones.com';  var SENDER = 'no-reply@precious-gemstones.com';    var response = {   "isBase64Encoded": false,   "headers": { 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': 'precious-gemstones.com'},   "statusCode": 200,   "body": "{\"result\": \"Success.\"}"   };    exports.handler = function (event, context) {      console.log('Received event:', event);      sendEmail(event, function (err, data) {          context.done(err, null);      });  };     function sendEmail (event, done) {      var params = {          Destination: {              ToAddresses: [                  RECEIVER              ]          },          Message: {              Body: {                  Text: {                      Data: 'name: ' + event.name + '\nemail: ' + event.email + '\nmessage: ' + event.message,                      Charset: 'UTF-8'                  }              },              Subject: {                  Data: 'Gemstone inquiry: ' + event.name,                  Charset: 'UTF-8'              }          },          Source: SENDER      };      ses.sendEmail(params, done);  }  

And here is my html code snippet:

                        <div class="contact-form">                              <form id="contact-form" method="post" action="https://a78agcql46.execute-api.us-east-1.amazonaws.com/email">                                  <input name="name" type="text" class="form-control" placeholder="Your Name" required>                                  <br />                                  <input name="email" type="email" class="form-control" placeholder="Your Email" required>                                  <br />                                  <textarea name="message" class="form-control" required rows="10">Your Message</textarea>                                  <br />                                  <input type="submit" class="form-control submit" value="Send inquiry">                              </form>              </div>              </div>                  </div>              </div>          </div>      </div>      <script src="https://code.jquery.com/jquery-3.5.1.min.js" integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script>      <script src="submitToAPI.js"></script>  </body>  

Any ideas? Thanks so much for the help!

https://stackoverflow.com/questions/65557523/any-ideas-on-api-gateway-url-error-ses-api-gateway-lambda-integration-with-ja January 04, 2021 at 11:59AM

没有评论:

发表评论