将跨源资源共享 (CORS) 标头添加到响应 - Amazon CloudFront
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

将跨源资源共享 (CORS) 标头添加到响应

如果响应尚未包含此标头,以下函数示例会将 Access-Control-Allow-Origin HTTP 标头添加到响应中。此标头属于跨源资源共享 (CORS) 的一部分。该标头的值 (*) 告诉 Web 浏览器允许来自任何源的代码访问此资源。有关更多信息,请参阅 MDN Web Docs 网站上的 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; }