我已经阅读了所有的解决方案,到目前为止我都试过了,没有一个有效.
我有一个ASP.NET Core8Web API后端和一个混合的.NET8/ANGLE 15前端.前端应用程序可以向API发出GET
个请求,但有POST
个请求被CORS阻止.
我已经在接口中try 了以下几种方式:
builder.Services.AddCors(options =>
{
options.AddDefaultPolicy(builder => {
builder.WithOrigins("http://localhost:1859");
builder.WithMethods("GET", "POST");
builder.AllowAnyHeader();
});
});
和
app.UseHttpsRedirection();
app.UseCors();
app.UseAuthorization();
与.一起
builder.Services.AddCors(options =>
{
options.AddDefaultPolicy(builder => {
builder.AllowAnyOrigin();
builder.AllowAnyMethod();
builder.AllowAnyHeader();
});
});
The controller method (I've tried with 和 without [EnableCors()]
):
[HttpPost]
public async Task<int> Save(SaveContractRequest req)
Angular 服务方法:
saveContract(req: SaveContractRequest) {
return this.http.post<number>(this.settingsSvc.apiEndpoint + ReimbursementEndPoints.CONTRACTS_SAVE, req);
}
然而,我总是得到这样的错误:
CORS策略已阻止在‘http://localhost/evms.api/api/Reimbursement/Contracts/Save’从源http://localhost:1859‘访问XMLHttpRequest.请求的资源上不存在"Access-Control-Allow-Origin"标头.
I'm at a loss. I can't get any more permissive than "allow any origin, any header, 和 any method". Why doesn't this work? Why can't I call a controller with a POST
method?