AWS API Gateway No 'Access-Control-Allow-Origin' header is present

后端 未结 5 1670
长发绾君心
长发绾君心 2020-12-05 00:31

I\'m stuck on an issue with API gateway and I\'ve gone through all the other SO answers on this, AWS forums and have been through their docs but still no joy.

I am t

5条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-05 00:59

    In case you want to change only one header instead of replacing all headers as is shown in Words Like Jared answer. You can use this code:

    'use strict';
    
    module.exports.handler = (event, context, callback) => {
      const response = event.Records[0].cf.response;
      const headers = response.headers;
    
      headers['access-control-allow-origin'] = [{ key: 'Access-Control-Allow-Origin', value: "*" }];
    
      return callback(null, response);
    };
    

    Another examples can be found on Adding HTTP Security Headers Using Lambda@Edge and Amazon CloudFront. It works same for normal Lambda function.

提交回复
热议问题