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?