我已经在.net core应用程序中实现了Cors策略:
在 ConfigureServices
下的 Startup.cs
中,我添加了以下 cors 策略
services.AddCors(options =>{
options.AddPolicy("CorsPolicy",
builder => builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials());
});
添加 CORS 策略后,我遇到一个奇怪的问题,在来自 UI 的每个 POST 调用中,都会进行两次调用:第一个调用返回 204,另一个调用返回带有 200 状态代码的数据。
最佳答案
第一个是 preflighted request 。主要目标是确定实际请求是否可以安全发送。跨站点请求会进行预检,因为它们可能会对用户数据产生影响。
A CORS preflight request is a CORS request that checks to see if the CORS protocol is understood.
It is an
OPTIONS
request using two HTTP request headers: Access-Control-Request-Method and Access-Control-Request-Headers, and the Origin header.A preflight request is automatically issued by a browser when needed.
这个HTTP access control (CORS)描述如果为真则请求被预检的条件。
关于asp.net-core - 对 Post 请求 : with http 204 and 200 进行两次调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45544064/