- Newest
- Most votes
- Most comments
In your Lambda, be sure to return the expected headers in the response:
export const handler = async (event) => { // Write to DynamoDB const response = { statusCode: 200, headers: { "Access-Control-Allow-Headers" : "Content-Type", "Access-Control-Allow-Origin": "https://www.example.com", // Be sure to add the correct origin here "Access-Control-Allow-Methods": "OPTIONS,POST,GET" }, body: JSON.stringify(myResponseObject), }; return response; };
Hello.
It seems that a CORS error is occurring. Have you configured CORS on the API Gateway side?
In this case, it seems that "Access-Control-Allow-Origin" is not allowed, so please try setting "Access-Control-Allow-Origin" to "*".
https://repost.aws/knowledge-center/api-gateway-cors-errors
https://docs.aws.amazon.com/apigateway/latest/developerguide/how-to-cors.html
Initially I was having issues on not getting the form data inserted into dynamoDB, the API gateway was not allowing the data to go to the lambda function, and I as getting an error code 500 so I redeployed the API Gateway, configured CORS as "*", did the same on the S3 bucket and Cloudfront, after that the data is being allowed on the API Gateway and it's being written successfully on DynamoDB. On Cloudwatch logs everything is fine no errors and everything is shown as successful but for some reason the javascript is not getting back the response code OK 200 so it thinks the operation was unsuccessful therefore it's not redirecting me to the thank-you.html
Since API Gateway is working without errors, I think the problem is with the front end. Can you check if the response from API Gateway includes CORS headers in your browser's developer tools?
Relevant content
- asked 5 years ago
- AWS OFFICIALUpdated a year ago
- AWS OFFICIALUpdated 6 months ago
- AWS OFFICIALUpdated 2 years ago
- AWS OFFICIALUpdated a year ago
It's working now many thanks! I included the above code but also looking at the API gateway's Method Response there was just 1 header configured(Access-Control-Allow-Origin), I added the other 2 and saved it, then redeployed the API gateway and voila :)