问题
So when I open a file that references a hosted ASP.NET Web API 2.0 project, I get the error:
Possible cross-origin (CORS) issue? The URL origin (https://secreturl.amazonaws.com) does not match the page (file://). Check the server returns the correct 'Access-Control-Allow-*' headers.
I only get answers for enabling cores for an HTTP request pipeline but not for (file://). I open the file from an index.html file, with path file:///C:/Users/PCName/desktop/index.html I assume the CORS have to be enabled in the Startup.cs file in the ASP.NET Core Web API 2.0
回答1:
Most browser implementations by default do not support CORS headers for local files (specifically they set the value to null which cannot then be used in an Access-Control-Allow header.)
The easiest thing to do is start a small server. If you've got Python installed, this is as easy as running python3 -m http.server 8000 in the C:/Users/PCName/desktop directory, and then you can browse to localhost:8000 (there are other 'instant servers' out there!).
That way you can use Access-Control-Allow-*.
来源:https://stackoverflow.com/questions/53443004/how-to-enable-cors-for-a-local-file-that-references-a-hosted-asp-net-web-api-tha