A developer is building a fitness tracking web application that retrieves user statistics by calling a REST API. The API is hosted on Amazon API Gateway and uses a Lambda Proxy integration. During testing, the web browser console displays a CORS error indicating that the 'Access-Control-Allow-Origin' header is missing. The developer has already enabled CORS on the API Gateway resources using the AWS Management Console, but the error remains. Which of the following is the correct action to resolve this issue?
- Modify the backend Lambda function to return the Access-Control-Allow-Origin header in its response JSON.Cevap
- BConfigure a CORS policy on the Amazon S3 bucket hosting the web application to allow requests from the API Gateway URL.
- CAdd the Access-Control-Allow-Origin header to the API Gateway Method Response configuration and redeploy the API.
- DCreate an IAM role that allows the API Gateway execution role to trust the Amazon S3 bucket hosting the application.
Cevap
Modify the backend Lambda function to return the Access-Control-Allow-Origin header in its response JSON.
Under a Lambda Proxy integration, Amazon API Gateway passes the backend response directly to the client without modifying the headers. Therefore, enabling CORS on the API Gateway resource only sets up the preflight OPTIONS endpoint. The developer must update the Lambda function code to return the Access-Control-Allow-Origin header in the headers key of the response JSON payload.
Adım Adım Çözüm
Anahtar Kavram
CORS handling in API Gateway Lambda Proxy integrations
Tahmini Süre:1m 0s