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