Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

423
Views
Static webpage in s3 bucket and making ajax request inside the page to call lambda function

I have uploaded a html page in s3 bucket and that html page makes an ajax post request to the api gateway url to send an email.

The problem is that the same api gateway url if I use postman to make a post request to it the email is sent but with the html page in s3 having ajax code doesn't work.

Any idea, or help will be helpful for me. Thanks ----Ajax code

    $.ajax("https://apigateway-url/email_sending", {
    "type": "POST",
    "data": JSON.stringify(formData),
    "contentType": "application/json"
    }).done(function () {
        console.log("Done")
    }).fail(function () {
        console.log("Failed");
        // console.log(data);
    });

I have tried so many help from net like edit cors configuration in s3 or enable cors in api gateway, but none worked for me.

NodeJs code for send Email

    var sendemail = require('./sendemail')
    app.post('/email_sending',function(req,res){
     console.log("Request received For sending mail")
     sendemail.send(req.body,function(err, data){

   // res.setHeader('Access-Control-Allow-Origin', '*');
    res.end("Success");
   })

  })

sendemail module snippet is :

    ses.sendemail(data, done)

I considered this website but didn't get the solution : https://codehabitude.com/2016/04/05/forms-to-emails-using-aws-lambda-api-gateway/

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Its very likely you aren't setting up the CORS headers correctly in apigateway. The apigateway documentation http://docs.aws.amazon.com/apigateway/latest/developerguide/how-to-cors.html clearly describes the process.

Once you've setup CORS and deployed the changes, you can verify the CORS preflight request using the below curl command

curl -H "Origin: http://example.com" \
  -H "Access-Control-Request-Method: POST" \
  -H "Access-Control-Request-Headers: X-Requested-With" \
  -X OPTIONS --verbose \
    https://apigateway-url/email_sending

Also, note that you need to deploy the changes after you setup CORS (Big blue Deploy API button - http://docs.aws.amazon.com/apigateway/latest/developerguide/how-to-deploy-api-with-console.html) before the change is reflected in your API

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!