两次对 Post 请求的调用:使用 http 204 和 200 [英] Two calls on Post request: with http 204 and 200

查看:115
本文介绍了两次对 Post 请求的调用:使用 http 204 和 200的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have implemented Cors policy in dot net core application: In Startup.cs under ConfigureServices I have added the following cors policy

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

在添加 CORS 策略后,我面临一个奇怪的问题,在来自 UI 的每次 POST 调用中,都会进行两次调用:第一次调用返回 204,其他调用返回状态码为 200 的数据.

I'm facing a strange issue after adding CORS policy, on every POST call from UI there are two calls made: first calls returns with 204 and other call returns the data with 200 status code.

推荐答案

第一个是 预检请求.主要目标是确定实际请求是否可以安全发送.跨站点请求是预检的,因为它们可能会对用户数据产生影响.

First one is a preflighted request. The main goal is to determinate whether the actual request is safe to send. Cross-site requests are preflighted since they may have implications to user data.

CORS 预检请求是一个检查 CORS 协议是否被理解的 CORS 请求.

A CORS preflight request is a CORS request that checks to see if the CORS protocol is understood.

这是一个 OPTIONS 请求,使用两个 HTTP 请求标头:Access-Control-Request-Method 和 Access-Control-Request-Headers,以及 Origin 标头.

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 访问控制 (CORS) 描述了以下条件如果为 true,则请求被预检.

This HTTP access control (CORS) describe conditions that if true then request is preflighted.

这篇关于两次对 Post 请求的调用:使用 http 204 和 200的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆