所有 PUT 请求都被 CORS 策略阻止
All PUT Requests are blocked by CORS policy
我有一个 .NET 5 API 和一些 cors-policies。
这是我的 CORS 设置:
public void ConfigureServices(IServiceCollection services)
{
services.AddOptions();
services.AddMemoryCache();
services.AddCors(options =>
{
options.AddPolicy("cors",
builder => builder
.WithOrigins(
"http://localhost:4200",
"http://127.0.0.1:4200",
"http://rev-staging.myhost.ch")
.WithMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
.AllowAnyHeader()
.AllowCredentials()
);
});
...
}
当然还有 app.UseCors("cors");
在 Startup.Configure(IApplicationBuilder, WebHostEnvironment )
当我在本地主机上执行我的方法时,一切正常。
一旦我发布到 api-rev-staging.myhost.ch
并在实时服务器上执行它们,对于所有 PUT 请求我都会得到
Access to XMLHttpRequest at 'http://api-rev-staging.myhost.ch/api/v1/Vacancies/7' from origin 'http://rev-staging.myhost.ch' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
(我更改了 URL 因为它是公司 URL)
所有 GET 和 POST 请求都有效 - 只有 PUT 请求无效。从 http://rev-staging.myhost.ch
上的 angular 前端执行时,或者从本地计算机上的邮递员执行时,它们都不起作用 - 但 GET 和 POST 仍然有效。
知道这里出了什么问题吗?
编辑:也许这很重要。后端在 IIS 上运行,与前端相同的服务器,只是另一个主机名。
尝试检查您的服务器是否接受 Web 的 PUT 和 DELETE API。
这些动词开箱即用。
我有一个 .NET 5 API 和一些 cors-policies。
这是我的 CORS 设置:
public void ConfigureServices(IServiceCollection services)
{
services.AddOptions();
services.AddMemoryCache();
services.AddCors(options =>
{
options.AddPolicy("cors",
builder => builder
.WithOrigins(
"http://localhost:4200",
"http://127.0.0.1:4200",
"http://rev-staging.myhost.ch")
.WithMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
.AllowAnyHeader()
.AllowCredentials()
);
});
...
}
当然还有 app.UseCors("cors");
在 Startup.Configure(IApplicationBuilder, WebHostEnvironment )
当我在本地主机上执行我的方法时,一切正常。
一旦我发布到 api-rev-staging.myhost.ch
并在实时服务器上执行它们,对于所有 PUT 请求我都会得到
Access to XMLHttpRequest at 'http://api-rev-staging.myhost.ch/api/v1/Vacancies/7' from origin 'http://rev-staging.myhost.ch' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
(我更改了 URL 因为它是公司 URL)
所有 GET 和 POST 请求都有效 - 只有 PUT 请求无效。从 http://rev-staging.myhost.ch
上的 angular 前端执行时,或者从本地计算机上的邮递员执行时,它们都不起作用 - 但 GET 和 POST 仍然有效。
知道这里出了什么问题吗?
编辑:也许这很重要。后端在 IIS 上运行,与前端相同的服务器,只是另一个主机名。
尝试检查您的服务器是否接受 Web 的 PUT 和 DELETE API。
这些动词开箱即用。