我正在使用 Angular 和 ASP.NET Core 构建一个网站。
\n在某些页面上,我想从 Web API 获取数据。当我运行该应用程序时,浏览器(Firefox)显示
\n\n\n跨源请求被阻止:同源策略不允许读取位于...(url)的远程资源(原因:CORS 标头 \xe2\x80\x98Access 中缺少令牌 \xe2\x80\x98authorization\xe2\x80\x99 -来自 CORS 预检通道的 Control-Allow-Headers\xe2\x80\x99)。
\n
我尝试了其他浏览器,得到了同样的错误。
\n出于授权考虑,我使用 aHttpInterceptor为来自 Angular 前端的每个请求插入授权标头。
然后我查看了 ASP.NET Core 后端。我将 CORS 策略设置为 app.UseCors(builder => { builder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader(); });,但仍然不起作用。
我用Postman测试了API,效果很好。
\n哪里出了问题?
\n文件Startup.cs。
public class Startup\n{\n public Startup(IConfiguration configuration)\n {\n Configuration = configuration;\n }\n\n public IConfiguration Configuration { get; }\n\n // This method gets called by the runtime. Use …Run Code Online (Sandbox Code Playgroud)