【发布时间】:2020-06-17 16:41:34
【问题描述】:
我在前端使用Angular,在后端使用C#,当我尝试编辑字段并保存时出现此错误:
Access to XMLHttpRequest at 'http://192.168.220.14:81/api/registry' from origin 'http://artc.tj' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
PUT http://192.168.220.14:81/api/reg polyfills-es2015.23e79c711e3c227d781f.js:1 PUT http://192.168.220.14:81/api/registry net::ERR_FAILED
Startup.cs:
public void ConfigureServices(IServiceCollection services)
{
//CORS
services.AddCors(options =>
{
options.AddPolicy(MyAllowSpecificOrigins,
builder =>
{
builder.WithOrigins("http://localhost",
"http://localhost:4200",
"http://artc.tj",
"http://192.168.220.14")
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials();
});
});
}
【问题讨论】:
-
响应的 HTTP 状态码是什么?您可以使用浏览器开发工具中的网络窗格进行检查。是 4xx 还是 5xx 错误而不是 200 OK 成功响应?
-
你在configure方法中添加
app.UseCors(MyAllowSpecificOrigins);了吗? -
我猜你注册中间件太晚了。见stackoverflow.com/a/60038513/5517088
标签: angular asp.net-core cors