AWS API Gateway需要在响应中返回一些特定的HTTP头,以满足浏览器对CORS的安全性要求。如果遇到这种问题,可以考虑在API Gateway中进行以下配置:
Access-Control-Allow-Origin: '*' Access-Control-Allow-Headers: 'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token'
Access-Control-Allow-Origin: '*' Access-Control-Allow-Headers: 'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token' Access-Control-Allow-Methods: 'DELETE,GET,HEAD,OPTIONS,PATCH,POST,PUT'
以下是使用AWS CLI配置API Gateway中CORS的代码示例:
aws apigateway update-rest-api --rest-api-id --patch-operations op='replace',path='/disableExecuteApiEndpoint',value='false'
aws apigateway put-method-response --rest-api-id --resource-id --http-method OPTIONS --status-code 200 --response-models '{}'
aws apigateway put-method-response --rest-api-id --resource-id --http-method --status-code 200 --response-models '{"application/json": "Empty"}'
aws apigateway put-integration-response --rest-api-id --resource-id --http-method --status-code 200 --response-parameters 'method.response.header.Access-Control-Allow-Origin=*' 'method.response.header.Access-Control-Allow-Headers=*' 'method.response.header.Access-Control-Allow-Methods=*'
其中,