I have created AWS lambda function for API request in python.
Client send API post request to lambda function using ajax with several parameters.
But I can’t get those parameters in lambda function.
The error is Access to XMLHttpRequest at 'https://xxxxx.execute-api.us-east-1.amazonaws.com/default/sssss' from origin 'http://127.0.0.1:5000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.
I have defined CORS policy in AWS lambda configuration.
Here is the AWS lambda function.
def lambda_handler(event, context):
if event['httpMethod'] == 'OPTIONS' or event['httpMethod'] == 'POST':
source = json.loads(event['body'])
response = {
'statusCode': 200,
'headers': {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*', # Set CORS to allow requests from any origin
'Access-Control-Allow-Headers': 'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token,X-Requested-With',
'Access-Control-Allow-Methods': 'GET,POST,PUT,DELETE,OPTIONS',
},
'body': json.dumps(f'{source['dias']}')
}
else:
response = {
'statusCode': 404, # Example status code for other methods
'body': json.dumps({'message': 'Method not allowed'})
}
return response
I have researched about this error.
They say that I need to add http statusok to response, but I can’t find exact code sample with lambda python code.
1