0

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!

  • 1
    What's your actual problem? – Dunedan May 11 '21 at 17:35
  • My frontend is not working, I am not sure whether my URL is getting triggered or not on my frontend. please help me out if there is anything wrong with my frontend code. – Shreyas Anil Chaudhari May 11 '21 at 17:38
  • See [how to debug ajax calls](https://stackoverflow.com/questions/21533285/why-the-ajax-script-is-not-running-on-iis-7-5-win-2008-r2-server/21617685#21617685) and [debugging jquery](https://www.simonbattersby.com/blog/debugging-jquery-a-beginners-guide/). – jarmod May 11 '21 at 17:54

0 Answers0