asp.net-core - 对 Post 请求 : with http 204 and 200 进行两次调用

标签 asp.net-core cors .net-core

我已经在.net core应用程序中实现了Cors策略: 在 ConfigureServices 下的 Startup.cs 中,我添加了以下 cors 策略

services.AddCors(options =>{
                options.AddPolicy("CorsPolicy",
                    builder => builder.AllowAnyOrigin()
                    .AllowAnyMethod()
                    .AllowAnyHeader()
                    .AllowCredentials());
            });

添加 CORS 策略后,我遇到一个奇怪的问题,在来自 UI 的每个 POST 调用中,都会进行两次调用:第一个调用返回 204,另一个调用返回带有 200 状态代码的数据。

two calls on post request

最佳答案

第一个是 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/

相关文章:

angular - 设置 Azure B2C 的正确方法?

c# - Visual Studio 2019不会在项目打开时提取所需的Docker镜像

razor - 索引 View 中的 ASP.NET Core 模型空错误

typescript - 获取 api 不返回 Location header

c# - 将 Cake dotnet 核心测试输出导出到 TeamCity

amazon-web-services - AWS Lambda HTTP API网关集成无法实现CORS

cors - WebStorm: 'Access-Control-Allow-Origin' header 包含多个值 'http://evil.com/

asp.net-mvc - asp.net核心开发模型

c# - 当我的 View 模型有构造函数时发布数据不起作用

c# - 读取解决方案数据文件 ASP.Net Core