如何为引用托管在 Amazon AWS 上的 ASP.NET Web API 的本地文件启用 CORS

How to enable CORS for a local file that references a hosted ASP.NET Web API that is hosted on Amazon AWS

因此,当我打开引用托管 ASP.NET Web API 2.0 项目的文件时,出现错误:

可能 cross-origin (CORS) 问题? URL 来源 (https://secreturl.amazonaws.com) 与页面 (file://) 不匹配。检查服务器 returns 是否正确 'Access-Control-Allow-*' headers.

我只得到了为 HTTP 请求管道启用核心的答案,但没有得到 (file://) 的答案。我从 index.html 文件打开文件,路径为 file:///C:/Users/PCName/desktop/index.html 我假设必须在 ASP.NET Core Web API 2.0

Startup.cs 文件中启用 CORS

默认情况下,大多数浏览器实现不支持本地文件的 CORS headers(具体来说,它们将值设置为 null,然后不能在 Access-Control-Allow header.)

最简单的事情就是启动一个小型服务器。如果你已经安装了 Python,这就像在 C:/Users/PCName/desktop 目录中 运行 python3 -m http.server 8000 一样简单,然后你可以浏览到 localhost:8000(有其他 'instant servers' 在那里!)。

这样你就可以使用 Access-Control-Allow-*.