응답에 교차 원본 리소스 공유(CORS) 헤더 추가 - Amazon CloudFront

응답에 교차 원본 리소스 공유(CORS) 헤더 추가

다음 뷰어 응답 함수는 응답에 이 헤더가 아직 포함되어 있지 않은 경우 응답에 Access-Control-Allow-Origin HTTP 헤더를 추가합니다. 이 헤더는 CORS(cross-origin 리소스 공유)의 일부입니다. 헤더의 값(*)이 모든 오리진의 코드가 이 리소스에 액세스 할 수 있도록 웹 브라우저에 지시합니다. 자세한 내용은 MDN 웹 문서 웹 사이트의 Access-Control-Allow-Origin을 참조하세요.

GitHub에서 이 예제를 참조하세요.

JavaScript runtime 2.0
async function handler(event) { const request = event.request; const response = event.response; // If Access-Control-Allow-Origin CORS header is missing, add it. // Since JavaScript doesn't allow for hyphens in variable names, we use the dict["key"] notation. if (!response.headers['access-control-allow-origin'] && request.headers['origin']) { response.headers['access-control-allow-origin'] = {value: request.headers['origin'].value}; console.log("Access-Control-Allow-Origin was missing, adding it now."); } return response; }
JavaScript runtime 1.0
function handler(event) { var response = event.response; var headers = response.headers; // If Access-Control-Allow-Origin CORS header is missing, add it. // Since JavaScript doesn't allow for hyphens in variable names, we use the dict["key"] notation. if (!headers['access-control-allow-origin']) { headers['access-control-allow-origin'] = {value: "*"}; console.log("Access-Control-Allow-Origin was missing, adding it now."); } return response; }