I am working on an automation project where I have to design a frontend for my AWS Lambda function which will be creating an entry in my AWS Dynamo DB table. I am using AWS HTTP API Gateway to create a link between the frontend and lambda function. Just FYI my backend i.e. lambda function and API URL is working fine as I checked that through Postman. I can send the data through API in dynamo DB through Postman. So I know there is nothing wrong there, only I am concerned with the frontend.
Frontend code:
<html>
<body>
<h2>Project Automation</h2>
<div id="entries">
</div>
<form>
<label for="projectId">Id:</label><br>
<input type="text" id="Id"><br>
<label for="name">Project Name:</label><br>
<input type="text" id="name"><br><br>
<button id="submitButton">Submit</button>
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script type="text/javascript">
$('#submitButton').on('click', function() {
$.ajax({
type: 'POST',
dataType: "json",
url: 'https://1wuscm3kr7.execute-api.us-east-1.amazonaws.com/prod/entries',
contentType: "application/json",
data: JSON.stringify({"Id": $('#Id').val()}),
data: JSON.stringify({"name": $('#name').val()}),
success: function(data) {
location.reload();
}
});
return false;
});
</script>
</body>
</html>
Lambda function:
console.log('starting function');
const AWS = require('aws-sdk');
const docClient = new AWS.DynamoDB.DocumentClient({region: 'us-east-1'});
exports.handler = function(e, ctx, callback) {
var params = {
Item: {
projectId: e.projectId,
code: e.code,
name: e.name,
license: e.license
},
TableName: 'ProjectTest'
};
docClient.put(params, function(err, data) {
if(err) {
callback(err, null);
} else{
callback(null, data);
}
});
}
Any help will be appreciated. Thanks!