-1

I'm using code from an aws doc to implement a contact form on a static site. When I test the nodejs lambda function it is successful but the result returned is null.

Same result with a POST test in the API gateway. It returns:

HTTP Status: 200
Response Body: null

For the Method Execution it says Models: application/json => Empty. Is the issue there? If so, I'm not sure what to change.

var AWS = require('aws-sdk');
var ses = new AWS.SES();

var RECEIVER = 'name@example.com';
var SENDER = 'name@example.com';

var response = {
 "isBase64Encoded": false,
 "headers": { 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': 'example.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 + '\nphone: ' + event.phone + '\nemail: ' + event.email + '\nnote: ' + event.note,
                    Charset: 'UTF-8'
                }
            },
            Subject: {
                Data: 'Website Contact Form: ' + event.name,
                Charset: 'UTF-8'
            }
        },
        Source: SENDER
    };
    ses.sendEmail(params, done);
}

From the html side, the contact form works as expected when I run the script (an email is delivered to my inbox with name, email, and message) but the ajax request returns an error status 0 HTTP Error: 0

function submitToAPI(e) {
       e.preventDefault();
       var URL = "https://Restful API URL";

            var name = /[A-Za-z]{1}[A-Za-z]/;
            if (!name.test($("#name-input").val())) {
                         alert ("Name cannot be less than 2 characters");
                return;

            var email = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,6})?$/;
            if (!email.test($("#email-input").val())) {
                alert ("Please enter valid email address");
                return;
            }

       var name = $("#name-input").val();
       var email = $("#email-input").val();
       var note= $("#note-input").val();
       var data = {
          name : name,
          email : email,
          note: note
        };

       $.ajax({
         type: "POST",
         url : "https://Restful API URL",
         dataType: "json",
         crossDomain: "true",
         contentType: "application/json; charset=utf-8",
         data: JSON.stringify(data),


         success: function () {
           alert("Thank you!");
           document.getElementById("contact-form").reset();
       location.reload();
         },
         error: function (response) {
           alert("HTTP Error: " + response.status);
           document.getElementById("contact-form").reset();
       location.reload();
         }});
     }

I've searched forums here and have tried deleting dataType: "json" and have also checked CORS is enabled. Neither fixed it. I'm stuck. Can anyone point me in the right direction?

Aleksndr
  • 1
  • 1

1 Answers1

0

Looks to me you are telling your lambda to return null in the callback. You can return your response variable instead:

exports.handler = function (event, context) {
    console.log('Received event:', event);
    sendEmail(event, function (err, data) {
        context.done(err, response); // <--- change null to response
    });
};
RobotEyes
  • 4,929
  • 6
  • 42
  • 57
  • Good eye. I made the change and it is now returning ```{ "isBase64Encoded": false, "headers": { "Content-Type": "application/json", "Access-Control-Allow-Origin": "example.com" }, "statusCode": 200, "body": "{\"result\": \"Success.\"}" } ``` But the html/jquery implementation is still returning http status 0 even though the form works. – Aleksndr Jun 11 '20 at 21:40
  • Is your ajax request getting an error? If so what is it? What's the response you are getting back? – RobotEyes Jun 11 '20 at 21:50
  • Yes the ajax request is returning HTTP Error: 0 – Aleksndr Jun 11 '20 at 21:58
  • It's worth checking off the other points from these answers in addition to the ones you say you attempted in your question https://stackoverflow.com/a/14507670/1449475 – RobotEyes Jun 11 '20 at 22:11
  • Thanks for digging that up. I'll have a look through. Appreciate your help with this. – Aleksndr Jun 11 '20 at 22:14