APIs enforce Cross-Origin Resource Sharing (CORS) to prevent unauthorized requests.
If you try to query an AWS Lambda API from your local machine (localhost) or a live website, the API must allow your origin.
In your Lambda function, make sure your HTTP response headers include:
{
"statusCode": 200,
"headers": {
"Access-Control-Allow-Origin": "https://jasoncv.click, http://localhost:8000",
"Access-Control-Allow-Methods": "POST,GET,OPTIONS",
"Access-Control-Allow-Headers": "Content-Type"
},
"body": "...your response..."
}
This allows your API to respond both from your live website and from a local server for testing.
To serve your HTML locally for testing, navigate to your project folder and run:
python -m http.server 8000
Then open your browser at http://localhost:8000/yourfile.html.
This simulates a live environment and allows your Lambda/API CORS rules to be tested with the local origin.
localhost in CORS headers for testing.preventDefault() in forms to avoid page reloads and allow proper API feedback.