Configure CORS response headers on AWS Lambda?

后端 未结 4 852
面向向阳花
面向向阳花 2020-12-10 01:22

I\'m trying to create a new service using AWS API Gateway, but I found out the browser automatically calls OPTIONS method in order to obtain CORS information.

The pr

4条回答
  •  鱼传尺愫
    2020-12-10 02:17

    If you have lambda-proxy enabled, you need to set the CORS headers manually:

    module.exports.hello = function(event, context, callback) {
    
        const response = {
          statusCode: 200,
          headers: {
            "Access-Control-Allow-Origin" : "*", // Required for CORS support to work
            "Access-Control-Allow-Credentials" : true // Required for cookies, authorization headers with HTTPS
          },
          body: JSON.stringify({ "message": "Hello World!" })
        };
    
        callback(null, response);
    };
    

    https://serverless.com/framework/docs/providers/aws/events/apigateway#enabling-cors

提交回复
热议问题