【发布时间】:2018-12-23 08:44:16
【问题描述】:
我正在尝试调用 WebAPI 方法来获取一些数据,但它会抛出 Cross-Origin Read Blocking (CORB) blocked cross-origin response https://localhost:44332/api/Controller/Action with MIME type text/html.,即使我在 ConfigureServices 方法中启用了 CORS 并在启动时的 Configure 方法中使用了该策略
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(options =>
{
options.AddPolicy("AllowAll",
builder =>
{
builder
.WithOrigins("http://localhost:4200/")
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials();
});
});
services.AddMvc();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
.....
app.UseCors("AllowAll");
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
可能是什么原因?
【问题讨论】:
-
localhost:4200 中的端口是否正确?尝试将端口更改为 44332
-
有时当 500 发生时,ajax 调用仍然会遇到 cors 错误。
-
该错误表示对
text/html的请求。应该是其他内容类型吗? -
@CRice 是正确的,这可能是因为您在处理 cors 之前遇到错误,也许您缺少依赖注入容器中的注册?
-
@AmanB 端口正确
标签: c# asp.net-core angular5 cross-origin-read-blocking