使用react应用程序POST时出现ASP.NET CORS异常



我有一个react应用程序在本地应用程序启用CORS的情况下调用我的服务。这对于GET方法来说很好,但由于某种原因,它在调用POST方法时会抛出CORS异常。我需要添加什么来配置POST吗?感谢

在我的启动.cs:

services.AddCors(options =>
{
options.AddDefaultPolicy(
builder =>
{
builder.WithOrigins(
"http://localhost:3000"
);
});
}); 

在我的react应用程序中,调用非常基本:

axios({
method: "POST",
url: `https://localhost:44340/patientsearch`,
data: { searchModel },
});     

例外:

Access to XMLHttpRequest at 'https://localhost:44340/patientsearch' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{            
app.UseCors(x =>
{
x.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader().WithOrigins("https://localhost:5002");
});           
}

最新更新