Azure 应用程序服务和 asp.net 核心:来源已被 CORS 策略甚至 AllowAnyOrigin 阻止
azure app service and asp.net core: origin has been blocked by CORS policy even AllowAnyOrigin
我使用 angular 8(前端)+ asp.net 核心 2.2(网络 api)开发了一个 SaaS 网络应用程序,我使用以下代码允许任何来源asp.net 核心上的 StartUp.cs 文件。在我部署它 azure 应用程序服务后,除了一页之外一切都很好,当我发送 post 请求时收到以下错误消息:
错误消息 return 到前端控制台:
对“https://api.xxxxxxxxx.com/api/stageProducts/addProduct' from origin 'https://xxxxxxxxx.com”处的 XMLHttpRequest 的访问已被 CORS 策略阻止:请求的资源上不存在 'Access-Control-Allow-Origin' header。
在 Startup.cs
app.UseCors(x => x.AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod());
能请教一下吗?
你能提供更多信息吗?比如你得到了什么错误代码,因为当你得到 500 服务器错误时,可能是你的代码有问题。您也可以在 Azure 上的应用服务中查看更详细的错误消息。
在您的 ConfigureServices 中试试这个
services.AddCors(options =>
{
options.AddPolicy("AllowAllOrigins",
builder => builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
);
});
在您的配置方法中执行以下操作
app.UseCors("AllowAllOrigins");
Access to XMLHttpRequest at 'https://api.xxxxxxxxx.com/api/stageProducts/addProduct' from origin 'https://xxxxxxxxx.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
请检查您是否 enabled CORS 在后端(网络 api)应用程序的 Startup.cs
中,如下所示。
app.UseCors(x => x.AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod());
app.UseHttpsRedirection();
app.UseMvc();
After I deployed it azure app service, all good except for one page, I got following error message when I send a post request
您正在 Azure 应用服务上托管您的后端(网络 api)应用,因此您还可以指定允许在 Azure 门户上对您的应用服务进行跨源调用的来源.
这是由其他一些与CORS无关的代码错误引起的。不知何故,asp.net 核心显示 CORS 错误消息。
我使用 angular 8(前端)+ asp.net 核心 2.2(网络 api)开发了一个 SaaS 网络应用程序,我使用以下代码允许任何来源asp.net 核心上的 StartUp.cs 文件。在我部署它 azure 应用程序服务后,除了一页之外一切都很好,当我发送 post 请求时收到以下错误消息:
错误消息 return 到前端控制台: 对“https://api.xxxxxxxxx.com/api/stageProducts/addProduct' from origin 'https://xxxxxxxxx.com”处的 XMLHttpRequest 的访问已被 CORS 策略阻止:请求的资源上不存在 'Access-Control-Allow-Origin' header。
在 Startup.cs
app.UseCors(x => x.AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod());
能请教一下吗?
你能提供更多信息吗?比如你得到了什么错误代码,因为当你得到 500 服务器错误时,可能是你的代码有问题。您也可以在 Azure 上的应用服务中查看更详细的错误消息。
在您的 ConfigureServices 中试试这个
services.AddCors(options =>
{
options.AddPolicy("AllowAllOrigins",
builder => builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
);
});
在您的配置方法中执行以下操作
app.UseCors("AllowAllOrigins");
Access to XMLHttpRequest at 'https://api.xxxxxxxxx.com/api/stageProducts/addProduct' from origin 'https://xxxxxxxxx.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
请检查您是否 enabled CORS 在后端(网络 api)应用程序的 Startup.cs
中,如下所示。
app.UseCors(x => x.AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod());
app.UseHttpsRedirection();
app.UseMvc();
After I deployed it azure app service, all good except for one page, I got following error message when I send a post request
您正在 Azure 应用服务上托管您的后端(网络 api)应用,因此您还可以指定允许在 Azure 门户上对您的应用服务进行跨源调用的来源.
这是由其他一些与CORS无关的代码错误引起的。不知何故,asp.net 核心显示 CORS 错误消息。